Archived topic
Disclosure statement insert in posts messed up since last Update
3 replies · Started by Gabriel on April 2, 2023
Hello, my affiliate disclosure insert on blog posts only is now messed up since the recent Gneratepress update. Basically, now instead of the disclosure statement showing after the first paragraph, it is showing at the very bottom of the copy.
I have included the code below. Your team provided this code a while back to someone else, but I modified it with my disclosure statement. Can you please tell me what's wrong?
The code is in Wordpress functions.php ( I use a child theme).
/* insert disclosure */
add_filter( 'the_content', 'db_prefix_first_h2_custom_html' );
function db_prefix_first_h2_custom_html( $content ) {
$html = '<div><p><small><strong>Quick disclosure:</strong> Some links on this page are affiliate links. We may earn revenue at no additional cost to you if you purchase using the links. We greatly appreciate your support.</small></p></div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $html, 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 );
}
/* Disclosure end */
Hi Gabriel,
For clarity, is the disclosure supposed to be after the first H2 or the first paragraph?
If it's after the first paragraph, try this instead:
add_filter( 'the_content', 'db_prefix_first_h2_custom_html' );
function db_prefix_first_h2_custom_html( $content ) {
$html = '<div><p><small><strong>Quick disclosure:</strong> Some links on this page are affiliate links. We may earn revenue at no additional cost to you if you purchase using the links. We greatly appreciate your support.</small></p></div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $html, 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 );
}
Worked perfectly. Thank you!
You're welcome, Gabriel!