-
GZeitung
Hello,
please allow us one more question:
How to insert a google ad before first h2 in blog posts and pages?
1 Ad should be displayed only before first h2
2 Would be great to be able to adjust distance to text and h2
3 Any chance to exclude specific pages like imprint?
Thank you!
-
Alvind
Hi there,
This plugin might do what you’re trying to achieve:
https://wordpress.org/plugins/ad-inserter/ -
GZeitung
Hello,
thank you but no way! Each plugin is one security issue more.
Is there no way to make it without plugin?
-
David
Hi there,
If you need a code snippet then you can try this:
add_filter('the_content', 'insert_custom_hook_before_first_h2'); function insert_custom_hook_before_first_h2($content) { if ( is_single() && !is_admin() ) { ob_start(); do_action('post_before_first_h2'); $custom_hook = ob_get_clean(); $pattern = '/<h2[^>]*>/i'; $replacement = $custom_hook . '$0'; $content = preg_replace($pattern, $replacement, $content, 1); } return $content; }
this will insert a custom hook inside the Post / Page content before the H2.
And you can access that hook in a GP Element by setting the Hook to Custom and in the field provided add:
post_before_first_h2
NOTE: this snippet uses
the_content
filter hook in WordPress, and so do many other plugins and functions, so theres no guarantee this will work as i cannot account for potential conflicts.
- You must be logged in to reply to this topic.