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.

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){
    $atts = shortcode_atts( array(
    'hook_name' => 'no foo'
    ), $atts, 'portable_hook' );
    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

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