最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Code to insert code into single php

programmeradmin1浏览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 );
}

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 );
}
Share Improve this question edited Mar 18, 2016 at 23:27 Snow asked Mar 18, 2016 at 22:17 SnowSnow 214 bronze badges 4
  • 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... – benomatis Commented Mar 18, 2016 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 Commented Mar 18, 2016 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 Commented Mar 18, 2016 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... :) – benomatis Commented Mar 19, 2016 at 7:12
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论