Is it possible to automatically add for example image before first h2 tag in the_content?
1 Answer
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– ChymmiCommented Jul 4, 2013 at 14:12
-
-
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").– PeterMCommented Mar 17, 2016 at 9:48