- This topic has 8 replies, 3 voices, and was last updated 1 year, 9 months ago by
David.
-
AuthorPosts
-
April 10, 2023 at 9:04 pm #2604917
nurban01
What’s the easiest way to insert my affiliate disclaimer (made in GP Elements) before the first H2?
I’ve tried a bunch of different approaches, including tweaking some of the other custom hooks mentioned in other threads. All the other threads are for hooking after the H2 (I usually have images there already).
This is basically the code I’ve been tweaking:
add_filter( 'the_content', 'insert_content_after_paragraphs', 20 ); function insert_content_after_paragraphs( $content ) { $html = do_shortcode( '[your-shortcode]' ); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $html, 1, $content ); } return $content; } // Parent Function that makes the magic happen function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</h2>'; $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 ); }
The code mentioned here for some reason puts the block at the bottom of my post. This one seemed useful, but I couldn’t get it to work either.
It’s difficult because my intro is sometimes 1 paragraph, sometimes 10, so I cannot use the after N paragraphs approach. Recently, I’ve put different sections of the posts in containers (so the first H2 is sometimes inside a container). I tried Ad Inserter, and had no problem with ads, but couldn’t figure out how to insert a GP element specifically.
Thank you!
April 10, 2023 at 9:41 pm #2604933Fernando Customer Support
Hi Nurban,
Can you try this?:
add_filter( 'the_content', 'insert_featured_image', 20 ); function insert_featured_image( $content ) { global $post; $inserted_hook = do_shortcode('[your-shortcode]'); 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 = '<h2>'; $paragraphs = explode( $closing_p, $content ); if ($paragraphs) { $paragraphs[0] = $paragraphs[0].$insertion; } return implode( '<h2>', $paragraphs ); }
April 10, 2023 at 10:30 pm #2604991nurban01
Thanks for the fast response, but that’s still going to the bottom of the post (you can see that in the link provided).
April 10, 2023 at 10:43 pm #2605003Fernando Customer Support
For reference, can you share a screenshot of the section added through the Shortcode?
Uploading Screenshots: https://docs.generatepress.com/article/using-the-premium-support-forum/#uploading-screenshots
April 10, 2023 at 10:48 pm #2605009nurban01
Well right now I am just leaving it as the placeholder and it appears in the same place. One of the examples I referenced earlier went *below* the first H2, which was the right idea. Ultimately I want to insert a GP Element I created. I am not even sure that a shortcode is the best way to do that.
April 26, 2023 at 12:36 pm #2621920nurban01
Any update? How else can I insert disclaimers into places near the middle of the body of the post? How can I modify this code to insert it before the H2? https://generatepress.com/forums/topic/element-after-first-paragraph/
April 26, 2023 at 7:32 pm #2622094Fernando Customer Support
Sorry, you’re response may have slipped through our system.
Can you share admin login credentials? We’ll try to take a closer look at the issue. Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
April 27, 2023 at 8:33 am #2622815nurban01
Hi, thank you. Credentials included below.
April 28, 2023 at 7:12 am #2623575David
StaffCustomer SupportHi there,
have you tried the one here:
https://generatepress.com/forums/topic/individual-hook/#post-2430170
-
AuthorPosts
- You must be logged in to reply to this topic.