Code to insert code into single php

Question

I’m using a plugin that puts stuff on my single.php page. I can turn off this stuff by inserting <!--code--> in the text side of the editor for any single post.

I want to do <!--code--> for all the single posts. I have code I previously used to insert an ad after the second paragraph (below). But when I take out the ad code and put in <!--code--> it doesn’t seem to in insert it like it would if I did it in the text side of a post ie nothing changes.

How can I insert a piece of code like <!--code--> as if in the text side of the WordPress editor, using this code or any other code?

(EDIT for clarity: The reader doesn’t see it. It goes in the “text” side of the post editor (rather than the “visual” side, and so its there, but the reader doesn’t see it and it isn’t visible on the “visual” side of the editor either.)

Thanks

// Insert ads after second paragraph of single post content. 

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
    $ad_code = '<div> **code** </div>'; 
    if ( is_single() && ! in_category( '1293' ) && ! is_admin() )  {
        return prefix_insert_after_paragraph( $ad_code, 2, $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 );
}
0
Snow 1 year 2020-07-10T16:10:20-05:00 0 Answers 64 views 0

Leave an answer

Browse
Browse