0

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 );
}
  • do you want to actually make <!--code--> appear to the reader? what is your goal with this? that didn't really come through, at least not to me... – webeno Mar 18 '16 at 22:43
  • Sorry for not being clearer on that. I'll edit my question, too. 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. – Snow Mar 18 '16 at 23:27
  • Sorry @snow but I don't understand what you're trying to do/override. If no one pipes in, maybe try simplifying the question: with what's happening now and what you want to happen. Also what is the plugin you're using that you mentioned at the top? – Will Mar 18 '16 at 23:56
  • That didn't make it much clearer to me, sorry, still not sure what you actually are trying to achieve with this, but @majick may be lucky and got it... :) – webeno Mar 19 '16 at 7:12
0

Confusing question. It sounds like you mean the plugin is checking for <!-- code --> in the raw post content and using this as a trigger to indicate not to add the ad code.

I think you need to identify the filter that the plugin is using to add the ad code so you can find the filter priority, and make sure you add your own filter before that runs. You could try this, I am using 0 priority here so it hopefully runs before anything else anyway:

add_filter('the_content','insert_ad_removal_code',0);
function insert_ad_removal_code($content) {
    // do not need !is_admin with these conditions
    if ( is_single() && in_category( '1293' ) )  {
        // maybe not neeed, but check if already added
        if (!strstr($content,'<!-- code -->')) {
            // add the code that will prevent the add
            $content = '<!-- code -->'.$content;
        }
    }
    return $content;
}

I think you are wanting to remove all ads from posts in category 1293 this way, but it is unclear maybe you want it the other way round.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.