Site logo

Archived topic

Is it possible to hook a block within the content?

9 replies · Started by Andrew on November 16, 2022

Viewing posts 1–10 of 10

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!

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

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 );.

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!

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)

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

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 );
}

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 '

This archived topic is closed to new replies.