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


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' );
  • 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 '16 at 9:48

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.