Just use below code. It will replace …
function replace_content($content)
{
$content = str_replace('#TEXT A#', '<div>#TEXT A#<span>#TEXT A-1#</span></div>',$content);
$content = str_replace('#TEXT B#', '<div>#TEXT B#<span>#TEXT B-1#</span></div>',$content);
$content = str_replace('#TEXT C#', '<div>#TEXT C#<span>#TEXT C-1#</span></div>',$content);
return $content;
}
add_filter('the_content','replace_content');
thanks, Venugopal!
I already know that code will work…
but I have so many text to replace, so I wanted to make it more efficient.
like,
function replace_content($content) {
$replace_pattern = '#text A-01#' , '#text A-02#';
$replace_pattern = '#text B-01#' , '#text B-02#';
$replace_pattern = '#text C-01#' , '#text C-02#';
$content = str_replace('$replace_pattern(text01)', '<div>$replace_pattern(text01)<span>$replace_pattern(text02)</span></div>',$content);
return $content;
}
add_filter('the_content','replace_content');
Instead of just adding the entire replacing code, I want to add the ‘$replace-pattern’ so it would be easier to manage.
I don’t even know how or if there’s any way to do this…
thanks again!
Welcome , Please find one more function i hope it will help you
Just you pass find replace words in array way
ex: array(“find word”=>”Replace words”);
function replace_content($content)
{
$find_replace = array( "#TEXT A1#" => "<div>#TEXT A#<span>#TEXT A-1#</span></div>","#TEXT A2#" => "<div>#TEXT A-02#<span>#TEXT A-2#</span></div>", "#TEXT B1#" => "<div>#TEXT B1#<span>#TEXT B-1#</span></div>" );
echo strtr( $content, $find_replace );
}
add_filter('the_content','replace_content');
Thanks
thank you very much Venugopal!
Apologies that I didn’t explain more specific.
The reason why I am trying to find a efficient way is:
for example, in a content, there will be a text “SOMETHING”,
and I want to replace it to be “<div>SOMETHING<span>EVERYTHING</span></div>”.
But each word is not the same. that is why I wanted to make a pattern.
so the code I wrote for was to explain that:
function replace_content($content) {
$replace_pattern = '#text A-01#' , '#text A-02#';
$replace_pattern = '#text B-01#' , '#text B-02#';
$replace_pattern = '#text C-01#' , '#text C-02#';
$content = str_replace('$replace_pattern(text01)', '<div>$replace_pattern(text01)<span>$replace_pattern(text02)</span></div>',$content);
return $content;
}
add_filter('the_content','replace_content');
to find “#text A-01#” in the content and replace it to be”<div>#text A-01#<span>#text A-02#</span></div>”
each text (like “#text A-01#”) has a matching text (like “#text A-02#”) to be replaced with.
Do you understand what I am trying to say??
Thank you very much for helping me!