Is there any way to use str_replace() in Wordpress outside the loop? I want to change my html markup which starts before the loop. I need something like this:

function content_magic($content) {
        str_replace('<div id="content">','<div id="new_content">',$content);
        return $content;

But this works only for markup within the loop. Please help me!

  • It would help to explain what you are trying to filter. There are filters in WordPress that affect nearly all the data that is output. Is it titles, menus, what? If you're dealing with data coming from the database, most likely there is a filter that will handle it. – goldenapples Jan 1 '11 at 20:08

Filters don't really affect resulting markup. More precisely they affect anything that is passed through them, in this specific case output of the_content() function is passed through the_content filter and you are able to modify it.

If markup you want to change is not generated by function and not passed through filter then most likely you will need to edit theme's template file that markup belongs to.

It is also good practice to create and use child theme to retain compatibility with theme updates and such if you are going to edit template.

  • Thanks. I check all filter hook and nothing is found. I was hoping that maybe there's some way to solve this problem, I was wrong. – Denis Belousov Jan 1 '11 at 21:55

Also, from a purely PHP point of view, the function str_replace returns a new string, and should be assigned to a different string variable. The examples are like below. See the PHP manual.

function content_magic($content) {
    $new_content = str_replace('<div id="content">','<div id="new_content">',$content);
    return $new_content;

Apparently, the str_replace function does not alter the original string.

  • Just pointed out the correct PHP syntax. – bbeeman Jan 1 '11 at 22:58
  • It's doesn't matter, because in my case I'm trying to filter markup that is outside the loop. There are only one way it is to edit my template. – Denis Belousov Jan 1 '11 at 23:18

It's probably unorthodox/unnecessary if you can change the templates, but under certain conditions it might be necessary. I realize this is an old post but just came across it and realized I actually do this a lot with output buffers. In the below example I'm removing the pingback link in the

You can use string replace or preg replace for more advanced replacements like the example below. Also I used !is_admin here but you can use advanced conditionals to target certain pages for replacement.

if (!is_admin()) {      
            function link_rel_buffer_callback($buffer) {
                $buffer = preg_replace('/(<link *?.*?\brel\b=["|\']\bpingback\b["|\'].*?\bhref\b(=["|\'](.*?)["|\'])*(.*?)(\/?>)(?=\s*?<))|(<link *?.*?\bhref\b(=["|\'](.*?)["|\'])*.*?\brel\b=["|\']\bpingback\b["|\'](.*?)(\/?>)(?=\s*?<))/', '', $buffer);
                return $buffer;

            function link_rel_buffer_start() {
            function link_rel_buffer_end() {
            add_action('template_redirect', 'link_rel_buffer_start', -1);
            add_action('get_header', 'link_rel_buffer_start');
            add_action('wp_footer', 'link_rel_buffer_end', 999);

Just use http://regex101.com/ to test your replacement expressions. So copy the rendered html from the page and paste it in the textarea and then type your regex to filter what you want changed.

All you need to change in the above example is the preg_replace output:

 $buffer = preg_replace('/original html goes here/', 'replacement html goes here', $buffer);

Then just add to the is admin to set different rules. Also if you dont need to target just use these actions instead.

            add_action('wp_head', 'link_rel_buffer_start', 999);
            add_action('wp_footer', 'link_rel_buffer_end', 999);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.