Site logo

[Support request] Is it possible to hook a block within the content?

Home Forums Support [Support request] Is it possible to hook a block within the content?

Home Forums Support Is it possible to hook a block within the content?

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2418594
    Andrew

    I’d like to hook a block element within the content area so readers see it as they scroll throughout each post. Ideally, I’d be able to position the block a certain distance down the post (i.e., 25% or 50% down the post, or after the first 10 paragraphs). Is this possible? If so, how?

    Thank you!

    #2418598
    Leo
    Staff
    Customer Support

    Hi Andrew,

    Take a look at this solution here:
    https://generatepress.com/forums/topic/element-after-first-paragraph/

    Let me know 🙂

    #2418622
    Andrew

    Awesome! It worked but how can I modify this so the hook show up after the 10th or 20th paragraph?

    #2418863
    Fernando
    Customer Support

    Hi Andrew,

    Yes, it should be possible.

    Try changing the value of 1 to your preference(ex. 10 or 20) in this line of code: return prefix_insert_after_paragraph( $inserted_hook, 1, $content );.

    #2515941
    Andrew

    hi Again,

    I have this all set up and currently have a block that hooks within the content after 10 paragraphs.

    Is there a simple way to add a second hook to the content, after 30 paragraphs for example? Would I need to copy/paste the code here https://generatepress.com/forums/topic/element-after-first-paragraph/ a second time?

    To be clear, I want two hooks within content. The one I already have after 10 paragraphs and another one after 30 paragraphs.

    Not sure if it matters but I want the same hook to appear twice.

    Thank you!

    #2515950
    Leo
    Staff
    Customer Support

    Would I need to copy/paste the code here https://generatepress.com/forums/topic/element-after-first-paragraph/ a second time?

    This should be the correct method.

    You would need to change the name of the hook (ie. after_first_paragraph) to a different name though.

    #2516471
    Andrew

    Thanks Leo – When I tried to paste the code again I get this error:

    Your PHP code changes were rolled back due to an error on line 126 of file wp-content/themes/prudentreviews/functions.php. Please fix and try saving again.

    Cannot redeclare insert_featured_image() (previously declared in wp-content/themes/prudentreviews/functions.php:95)

    #2516489
    Fernando
    Customer Support

    You just need to copy and paste this part:

    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, 1, $content );
        }
      

    But alter the function name and other stuff. Example:

    add_filter( 'the_content', 'insert_featured_image2', 20 );
    function insert_featured_image2( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_thirtieth_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 30, $content );
        }

    This hook should now be available with this new code: after_thirtieth_paragraph

    #2517658
    Andrew

    I just tried that and got the error “Your PHP code changes were rolled back due to an error on line 122 of file wp-content/themes/prudentreviews/functions.php. Please fix and try saving again.

    syntax error, unexpected end of file”

    This is the code I’m using, including the first part which is working fine.

    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, 10, $content );
    }
    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 );
    }
    add_filter( ‘the_content’, ‘insert_featured_image2’, 20 );
    function insert_featured_image2( $content ) {
    global $post;
    $inserted_hook = do_shortcode(‘[portable_hook hook_name=”after_fiftieth_paragraph”]’);
    if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $inserted_hook, 50, $content );
    }
    #2517767
    Fernando
    Customer Support

    At first glance at the code you pasted here, I can see that the quotation marks are slanted.

    It needs to be replaced with straight quotation marks.

    When copying and pasting, somethings, straight quotations are replaced with slanted quotations. This will cause issues.

    Please replace all slanted quotation marks with straight ones first. Example – This to this '

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