Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Google Ad before first h2

  • 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!

  • Hi there,

    This plugin might do what you’re trying to achieve:

  • Hello,

    thank you but no way! Each plugin is one security issue more.

    Is there no way to make it without plugin?

  • 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() ) {
            $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.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.