1

Is it possible to automatically add for example image before first h2 tag in the_content?

1 Answer 1

5

Yes, it is possible.

You can use the_content hook and assign your own filter to this hook.

Then you should (that is the part when things get a little bit messy) parse and replace content of that post.

So for example you can do it like so:

function my_the_content_filter($content) {
  $content = str_replace('<h2 ', '<img src=""...><h2 ', $content);
  return $content;
}

add_filter( 'the_content', 'my_the_content_filter' );
3
  • It says error :( Fatal error: Cannot pass parameter 4 by reference in
    – Chymmi
    Commented Jul 4, 2013 at 14:12
  • in...? The rest of this message is important. Commented Jul 4, 2013 at 14:39
  • Parameter 4 of str_replace is number of replaced tokens, not number of replacement. Remove it, or use some variable here. (Cannot edit post to fix this, as "Edit must be at elast 6 charaters").
    – PeterM
    Commented Mar 17, 2016 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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