- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Change the text on a wordpress page
hi – simple – looking for javascript that would change the text on wordpress page from “read more” to “view listing” ?
function replaceTextOnPage(from, to){
getAllTextNodes().forEach(function(node){
node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), ‘g’), to);
});
function getAllTextNodes(){
var result = [];
(function scanSubTree(node){
if(node.childNodes.length)
for(var i = 0; i < node.childNodes.length; i++)
scanSubTree(node.childNodes[i]);
else if(node.nodeType == Node.TEXT_NODE)
result.push(node);
})(document);
return result;
}
function quote(str){
return (str+”).replace(/([.?*+^$[]\(){}|-])/g, “\$1”);
}
}
replaceTextOnPage(‘read more’, ‘View Listing’);
function replaceTextOnPage(from, to){
getAllTextNodes().forEach(function(node){
node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), ‘g’), to);
});
function getAllTextNodes(){
var result = [];
(function scanSubTree(node){
if(node.childNodes.length)
for(var i = 0; i < node.childNodes.length; i++)
scanSubTree(node.childNodes[i]);
else if(node.nodeType == Node.TEXT_NODE)
result.push(node);
})(document);
return result;
}
function quote(str){
return (str+”).replace(/([.?*+^$[]\(){}|-])/g, “\$1”);
}
}
replaceTextOnPage(‘read more’, ‘View Listing’);
I suggest you don’t use Javascript for that, but the “proper” WordPress method instead: https://codex.wordpress.org/Customizing_the_Read_More