Site logo

Archived topic

Element after first paragraph

12 replies · Started by Cynthia on September 20, 2021

Viewing posts 1–13 of 13

Hello. I wanted my affiliate notice to show up in every post so I added a hook element that currently is set to generate after entry title. I want the notice of course to be before any of my affiliates are used but I don't love that it is the first thing people read. Is there a way to set it so that it will just come after my first paragraph in each post? I looked through the choices but I'm not very savvy so I'm probably just missing it. Please help :) Thanks
Cynthia

Hi Cynthia,

We can try making a hook after the first paragraph if you want.

Add this PHP snippet:

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

After activating this PHP snippet on your site, you can hook things on the first paragraph of the content using after_first_paragraph hook. :)

Hey Elvin,
Thanks for the help. I'm not very tech savvy. Would you suggest I use the plugin code snippets to add the PHP code you suggested or should I set up a child theme? Those were the two suggestions I saw when I was searching how to add PHP code in generate press. Thanks in advance :)
Cynthia

Any of the two choices should be fine but for your case, I'd suggest Code Snippets plugin as it's easier to setup. :D

Hey there. I finally got around to trying this and I'm screwing up somewhere because it's not working for me. So I went with your advice and added the snippet plug-in. I then did "add new" as a css and added the code you gave me above (I'm assuming that is what I should have added it as). Then I am given the choice of run snippet everywhere (which I chose), only run in admin area, only run on site front end, only run once. Then I went in under "elements" and tried to hook to the first paragraph. However I'm not seeing after_first_paragraph option. Could you help me where I am going wrong. Thank you!

I then did “add new” as a css and added the code you gave me above (I’m assuming that is what I should have added it as).

The code provided is a PHP snippet. It should be added as PHP. :)

However I’m not seeing after_first_paragraph option. Could you help me where I am going wrong. Thank you!

It won't show up on the display rule because it's a custom hook we've manually added.

On the "Hook" dropdown of the element, you should select "Custom". A field will appear. That's were you type in after_first_paragraph :D

Yay it worked! Thanks Elvin so much for your patience and kindness!

:)

Nice one! Glad you got it to work. Thanks for letting us know. No problem. :D

Hi there,

thanks for that.

I'm having an issue when adding this.

You can see the element The Why trust us box after the first paragraph. There's a table of content that is being nested with the block element. The table of content should be shown after that box.

https://stroller-envy.com/uppababy-vista/

I will reply to your topic

Sorry, I mean to post this reply on another thread. Is there a way to delete this reply?
———————-

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

Hi Andrew,

We'll reply in the topic you created.

This archived topic is closed to new replies.