I'm using str_replace
to search and replace some shortcodes as [warning]
with an html code <span class="warn_class"> Warning</span>
Here is my code
function replace($text) {
$text = str_replace('[warning]', '<span class="warning_class">Warning </span>', $text);
}
add_filter('the_content', 'replace');
As I need to explain to users how to use these shortcodes I'm trying to escape replacing the shortcode by using a backslashe before it\[warning]
. Here is my new code
function replace($text) {
$pattern = array();
$pattern[0]= '[warning]';
$pattern[1]= '\[warning]';
$replacement = array();
$replacement[0] = '<span class="warning_class"> Warning <span>';
$replacement[1] = '[warning]';
$text = str_replace($pattern, $replacement, $text);
}
add_filter('the_content', 'replace');
The problem is that all instances of [warning]
is being replaced.
Any idea to solve this problem?