WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there possible to automatically add for example image before first h2 in the_content ? Thank you

share|improve this question
up vote 5 down vote accepted

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, 1);
  return $content;

add_filter( 'the_content', 'my_the_content_filter' );
share|improve this answer
It says error :( Fatal error: Cannot pass parameter 4 by reference in – Chymmi Jul 4 '13 at 14:12
in...? The rest of this message is important. – Krzysiek Dróżdż Jul 4 '13 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 Mar 17 at 9:48

Your Answer


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.