Archived topic
Custom Hook BEFORE First H2 Heading (Affiliate Disclaimer)
8 replies · Started by nurban01 on April 10, 2023
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!
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 );
}
Thanks for the fast response, but that's still going to the bottom of the post (you can see that in the link provided).
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
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.
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/
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
Hi, thank you. Credentials included below.
Hi there,
have you tried the one here:
https://generatepress.com/forums/topic/individual-hook/#post-2430170