Site logo

[Support request] Disclosure statement insert in posts messed up since last Update

Home Forums Support [Support request] Disclosure statement insert in posts messed up since last Update

Home Forums Support Disclosure statement insert in posts messed up since last Update

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2593419
    Gabriel

    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 */
    #2593769
    Fernando
    Customer Support

    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 );
    }
    #2594018
    Gabriel

    Worked perfectly. Thank you!

    #2594105
    Fernando
    Customer Support

    You’re welcome, Gabriel!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.