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 WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Insert block after “n” paragraph, both pages and articles

  • Hello,

    I would like to insert automatically a block after “n” paragraph in my pages and in my articles (same number of paragraph for both)

    I’v found in this forum this code for articles :

    add_shortcode('portable_hook', function($atts){
    ob_start();
    $atts = shortcode_atts( array(
    'hook_name' => 'no foo'
    ), $atts, 'portable_hook' );
    do_action($atts['hook_name']);
    return ob_get_clean();
    });
    add_filter( 'the_content', 'insert_featured_image', 20 );
    function insert_featured_image( $content ) {
    global $post;
    $inserted_hook = do_shortcode('[portable_hook hook_name="after_first_paragraph"]');
    if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $inserted_hook, 5, $content ); //Changer le chiffre pour indiquer le paragraphe après lequel le bloc est inséré
    }
    return $content;
    }
    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 );
    }

    It works perfectly for articles. Is it possible to change it for applying it both on pages and articles ?

    Have a nice day !

  • Hi there,

    this line:

    if ( is_single() && ! is_admin() ) {

    Change it to:

    if ( ( is_page() || is_single() ) && ! is_admin() ) {

  • Hello David,

    Thanks for your answer. Unfortunatly, it does not seem to work. Block appears in articles but not in pages.

  • Hi Ben,

    For reference, could you share a link to a page where that should work on your site?

  • Hi Fernando

    Yes I put links in the private info of this message

  • Did you set the GP Element to display on pages too ?

  • Oups, yeah that’s it. I’ve set the pages in exclude, not include … Thanks for your help !

    And could it be possible to set more than one block in the same page / article ?

  • In that original snippet there is the function that filters the_content
    you can create another function to add a different hook in a different place

    For example:

    
    add_filter( 'the_content', 'insert_custom_hook_one', 20 );
        function insert_custom_hook_one( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_another_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 10, $content ); //Changer le chiffre pour indiquer le paragraphe après lequel le bloc est inséré
        }
        return $content;
    }
    

    1. we use a new function name here: eg. insert_custom_hook_one

    add_filter( 'the_content', 'insert_custom_hook_one', 20 );
        function insert_custom_hook_one( $content ) {

    2. here we define a new hook name eg. after_another_paragraph
    $inserted_hook = do_shortcode('[portable_hook hook_name="after_another_paragraph"]');

    3. and here we set the nth paragraph to 10
    return prefix_insert_after_paragraph( $inserted_hook, 10, $content ); here we dei

  • Thanks a lot David. I forget to change the name of the second function, that’s why I had an error …

    Thanks again for your big help !

  • You’re welcome

  • Hello @David, I have an another question about this code : I wish to exclude some pages in order the custom block does not appear on those pages

    I modify the code with that :

    $excluded_ids = array('4572', '11');
    if (((is_page() || is_single()) && !is_admin()) && !in_array(get_the_ID(), $excluded_ids))  {

    Is this ok and safe to do like this ? (it works but like I’m not use to php code, I would like the confirmation of an expert 🙂 )

  • That method is perfectly fine.
    But if you’re using a GP element to hook content into your custom hook, then you could simply edit the elements display rules and exclude the pages there.

  • @David : Haha you’re rigth, considerably easier ! 😀

    And I would like you’re advise : I’ve found a plugin, “adinserter”. I think it could be useful too for insert blocks inside articles, etc… but I have to code the layout I wish in HTML/CSS

    So, is this possible to create a specific block with GenerateBlocks and have a shortcode for it ? (because I can use shortcode with this plugin)

    Should be great for making easily “read more” boxes inside articles, custom ads in sidebar or anywhere else, etc … 🙂

  • Hi, thanks for the link , Ying

  • You are welcome   🙂

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