I would like to replace few words in the content of the posts. For example.
Original:
Here is my test string.
Which Need to Replace First Test word in the string.
Excepted String:
Here is my <a href="link"> test </a> string.
Which Need to Replace First Test word in the string.
I have tried but there are few scenario where i have faced issues.
When that word has predefined link along with a word. Like
Here is my <a href="test case link"> test Case </a> string.
Which Need to Replace First Test word in the string.
So my function exploded it in tow words like
Here is my <a href="link"> test </a> <a href="test case link"> Case </a> string.
Which Need to Replace First Test word in the string.
== My Solution ====
add_filter('the_content' , 'update_content');
function update_content($content){
if((is_singular()){
$termsList = array(
'test' => '<a href="test">test</a>'
);
foreach($termsList as $word => $term_link){
//$content = str_replace($word , $replacement , $content , $count);
//Convert into Smaller Case
$str_content = strtolower($content);
$str_word = strtolower($word);
//Get the position of the word
$position = strpos($str_content, $str_word , 0);
//Get the Exact word with case sensitive
$match_string = substr ($content , $position , strlen($word) );
//Prepare Replacement
$replacement = '<a href="'.$term_link.'">'.$match_string.'</a>';
if(!empty($position)){
//Replace The word
$content = substr_replace($content, $replacement, $position, strlen($word));
}
}
}
return $content;
}
I would like to replace few words in the content of the posts. For example.
Original:
Here is my test string.
Which Need to Replace First Test word in the string.
Excepted String:
Here is my <a href="link"> test </a> string.
Which Need to Replace First Test word in the string.
I have tried but there are few scenario where i have faced issues.
When that word has predefined link along with a word. Like
Here is my <a href="test case link"> test Case </a> string.
Which Need to Replace First Test word in the string.
So my function exploded it in tow words like
Here is my <a href="link"> test </a> <a href="test case link"> Case </a> string.
Which Need to Replace First Test word in the string.
== My Solution ====
add_filter('the_content' , 'update_content');
function update_content($content){
if((is_singular()){
$termsList = array(
'test' => '<a href="test">test</a>'
);
foreach($termsList as $word => $term_link){
//$content = str_replace($word , $replacement , $content , $count);
//Convert into Smaller Case
$str_content = strtolower($content);
$str_word = strtolower($word);
//Get the position of the word
$position = strpos($str_content, $str_word , 0);
//Get the Exact word with case sensitive
$match_string = substr ($content , $position , strlen($word) );
//Prepare Replacement
$replacement = '<a href="'.$term_link.'">'.$match_string.'</a>';
if(!empty($position)){
//Replace The word
$content = substr_replace($content, $replacement, $position, strlen($word));
}
}
}
return $content;
}
This is a fairly complex PHP (rather than WP) issue if you want to take into account all possibilities.
If you replace test
with <a href="#">test</a>
you will run into problems if the content already has <a href="?">test</a>
, because the result will then be <a href="?"><a href="#">test</a></a>
So, before you do the replace you will need to see if test
is already a link. This is not that simple, because you cannot simply scan for <a href="...">test</a>
. After all, you may have something like <a href="...">this is my test, yeah!
. Or test
may be a substring of another word, like protesting
. To prevent the latter being targeted you might check for test
, but then you may miss test,
.
Another thing to consider: if there already is a link around test
, do you want to keep that one or replace it?
So, depending on you needs you will have to build a regular expression that does complex pattern recognition on your content to make sure it does exactly what you need. For help with regular expressions, try Stack Overflow.