Site logo

Archived topic

Code no longer working

1 reply · Started by Danielle on July 16, 2021

Viewing posts 1–2 of 2

Hi,

about half a year ago David kindly created a PHP snippet for me where I could add shortcode to every post on my blog and it was great! However this snippet no longer works for me is there anything I need to change?

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, 3, $content );
    }
    return $content;
}
// Parent Function that makes the magic happen
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 );
}

Thank you!

Hi there,

nothing has changed that would affect how that code behaves.

Do you have any cache plugins or server page caching that may be interfering with it working ?

This archived topic is closed to new replies.