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.

I'm trying to filter my content and on matches on flow I would like to change my html structure, but my rule doesn't really apply. I have the following filter which searches the first image of posts and on rule match attaching a css container. The preg_match_all is working but the str_replace doesn't

function imageFooter($content){

      global $post, $posts;

      preg_match_all('/<a.href="(.*?)"><img.*?src="(.*?)".*?><\/a>/', $post->post_content, $matches);
      $to_search = $matches[0][0];
      $replacement = '<div class="image_footer">'.$matches[0][0].'<span class="logo"></span></div>';
      str_replace($to_search , $replacement, $post->post_content);
      return $content;

    }

    add_filter('the_content',   'imageFooter');
share|improve this question

2 Answers 2

up vote 0 down vote accepted

Change,

str_replace($to_search , $replacement, $post->post_content);

to..

$content =  str_replace($to_search , $replacement, $post->post_content);
share|improve this answer
    
Thanks a lot. This was an ordinary big mistake what I made –  fefe Feb 15 '13 at 15:06
    
No problem, also you are passing $content variable to your function by reference so you don't technically need your globals, you can of course just use $content. –  userabuser Feb 15 '13 at 15:10
    
Yes I have been changing theme as @s_ha_dum suggested –  fefe Feb 15 '13 at 15:11
    
Good stuff.. all the best. –  userabuser Feb 15 '13 at 15:14

You haven't set $content to anything so it is just returning the same thing that was passed into the function. Read the doc's for str_replace. You should have:

$content = str_replace($to_search , $replacement, $post->post_content);

But why are you operating on $post->post_content? The post content is passed into the filter. You even have a parameter named $content that you aren't using. What you are doing is likely to cause trouble. Think about this.

Your site has 4 filters on the_content including yours.

  1. Uses $content
  2. Uses $content as modified by filter one, since they "chain".
  3. Uses $content as modified by the previous two filters.
  4. Your filter uses $post->content which is outside of the "chain". You've just wiped out all the preceding filters.

Also, you aren't using $posts at all.

Your code should be:

function imageFooter($content){
      preg_match_all('/<a.href="(.*?)"><img.*?src="(.*?)".*?><\/a>/', $content, $matches);
      $to_search = $matches[0][0];
      $replacement = '<div class="image_footer">'.$matches[0][0].'<span class="logo"></span></div>';
      $content = str_replace($to_search , $replacement, $content);
      return $content;
}

add_filter('the_content',   'imageFooter');

And you should be warned that using regex to parser HTML is very dicey business. It is easy to go wrong.

share|improve this answer

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.