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
|
1 Answer
Reset to default 1Confusing 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.
<!--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