[Support request] Custom Hook BEFORE First H2 Heading (Affiliate Disclaimer)

Home Forums Support [Support request] Custom Hook BEFORE First H2 Heading (Affiliate Disclaimer)

Home Forums Support Custom Hook BEFORE First H2 Heading (Affiliate Disclaimer)

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #2604917
    nurban01

    What’s the easiest way to insert my affiliate disclaimer (made in GP Elements) before the first H2?

    I’ve tried a bunch of different approaches, including tweaking some of the other custom hooks mentioned in other threads. All the other threads are for hooking after the H2 (I usually have images there already).

    This is basically the code I’ve been tweaking:

    add_filter( 'the_content', 'insert_content_after_paragraphs', 20 );
    function insert_content_after_paragraphs( $content ) {
        $html = do_shortcode( '[your-shortcode]' );
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $html, 1, $content );
        }
        return $content;
    }
    // Parent Function that makes the magic happen
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '</h2>';
        $paragraphs = explode( $closing_p, $content );
        foreach ($paragraphs as $index => $paragraph) {
            if ( trim( $paragraph ) ) {
                $paragraphs[$index] .= $closing_p;
            }
            if ( $paragraph_id == $index + 1 ) {
                $paragraphs[$index] .= $insertion;
            }
        }
        return implode( '', $paragraphs );
    }

    The code mentioned here for some reason puts the block at the bottom of my post. This one seemed useful, but I couldn’t get it to work either.

    It’s difficult because my intro is sometimes 1 paragraph, sometimes 10, so I cannot use the after N paragraphs approach. Recently, I’ve put different sections of the posts in containers (so the first H2 is sometimes inside a container). I tried Ad Inserter, and had no problem with ads, but couldn’t figure out how to insert a GP element specifically.

    Thank you!

    #2604933
    Fernando
    Customer Support

    Hi Nurban,

    Can you try this?:

    add_filter( 'the_content', 'insert_featured_image', 20 );
    function insert_featured_image( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[your-shortcode]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 1, $content );
        }
        return $content;
    }
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '<h2>';
        $paragraphs = explode( $closing_p, $content );
        if ($paragraphs) {
            $paragraphs[0] = $paragraphs[0].$insertion;
        }    
        return implode( '<h2>', $paragraphs );
    }
    #2604991
    nurban01

    Thanks for the fast response, but that’s still going to the bottom of the post (you can see that in the link provided).

    #2605003
    Fernando
    Customer Support

    For reference, can you share a screenshot of the section added through the Shortcode?

    Uploading Screenshots: https://docs.generatepress.com/article/using-the-premium-support-forum/#uploading-screenshots

    #2605009
    nurban01

    Well right now I am just leaving it as the placeholder and it appears in the same place. One of the examples I referenced earlier went *below* the first H2, which was the right idea. Ultimately I want to insert a GP Element I created. I am not even sure that a shortcode is the best way to do that.

    #2621920
    nurban01

    Any update? How else can I insert disclaimers into places near the middle of the body of the post? How can I modify this code to insert it before the H2? https://generatepress.com/forums/topic/element-after-first-paragraph/

    #2622094
    Fernando
    Customer Support

    Sorry, you’re response may have slipped through our system.

    Can you share admin login credentials? We’ll try to take a closer look at the issue. Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information

    #2622815
    nurban01

    Hi, thank you. Credentials included below.

    #2623575
    David
    Staff
    Customer Support
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.