Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
    }
add_filter('the_content','content_magic');

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

share|improve this question
    
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

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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