Black Friday! Get up to 25% off GP Premium! Learn more ➝

[Resolved] Insert element in to blog posts

Home Forums Support [Resolved] Insert element in to blog posts

Home Forums Support Insert element in to blog posts

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #763407
    Dana

    Is it possible to use a hook element to auto insert something in to blog posts after x number of paragraphs?
    Thanks!

    GeneratePress 2.2.1
    GP Premium 1.7.7
    #763588
    David
    Staff
    Customer Support

    Hi there,

    you can’t hook the content but you can filter the_content – try this PHP snippet it adds a block of HTML after the second paragraph to all posts.

    add_filter( 'the_content', 'insert_content_after_p', 20 );
    function insert_content_after_p( $content ) {
        ob_start();
        ?>
        <div>HTML goes here...</div>
        <?php
        $after_p_content = ob_get_clean();
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $after_p_content, 2, $content );
        }
        return $content;
    }
    // Parent Function that makes the magic happen
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '</p>';
        $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 );
    }
    #887220
    Dana

    Hi David, Sorry life got busy! Where would I add this to make it work please?

    #887590
    David
    Staff
    Customer Support

    It just takes over sometimes 🙂
    Its PHP – this article covers the options:

    https://docs.generatepress.com/article/adding-php/

    #888486
    Dana

    Yay! I used the code snippet plugin (because the functions.php on my child theme wouldn’t save properly) and it worked perfectly!
    Thank you

    #889190
    David
    Staff
    Customer Support
    #1813230
    IBNU AHMAD

    Hi David,

    The solutions works!

    But it can only be used once.

    I tried creating new snippet, but gotten this error:

    The snippet has been deactivated due to an error on line 3:
    Cannot redeclare function insert_content_after_p.

    Anything I should do differently?

    #1813366
    David
    Staff
    Customer Support

    Hi there,

    thats correct you can only register a function once.
    If you want to add more inserts then only duplicate this part of the code:

    add_filter( 'the_content', 'insert_content_after_p', 20 );
    function insert_content_after_p( $content ) {
        ob_start();
        ?>
        <div>HTML goes here...</div>
        <?php
        $after_p_content = ob_get_clean();
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $after_p_content, 2, $content );
        }
        return $content;
    }

    In this code you will see 2 instances of:

    insert_content_after_p

    In your duplicate function you will need to change them both to something else eg. insert_content_after_p_number_two

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