I've currently got my website set up where it automatically adds a Google Adsense ad after the 2nd paragraph of any article, but I'd like to improve on this, if anyone is able to help.
I'd like to add to this code to add another 2 adverts; one after a 6th paragraph and another after a 10th. If the article doesn't reach these paragraph numbers then the adverts shouldn't show.
It's probably something really obvious but anything I've tried has resulted in the functions.php file crashing when I reload the site.
My code is...
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div class="mobilead .visible-xs-block hidden-sm hidden-md hidden-lg"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXX"
data-ad-slot="1716361890"
data-ad-format="auto"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>';
if ( is_single() && ! 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 );
}
As a supplementary question - is there a way to limit these ads to showing only on posts, rather than pages as well? Currently they're showing anywhere.
Any help would be hugely appreciated.
There is too much wrong with your ad code for me to attempt guessing what it should be (it has an opening <div> but no closing </div>, it has what appears to be javascript outside of a <script> tag)
...so I skip that part, and simply show how to insert another paragraph instead - this will insert something in the spots you want, and also shows how to use get_post_type() to ensure ads are only shown on posts:
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
//The last condition here ensures that ads are only added to posts
if ( is_single() && !is_admin() && get_post_type() === 'post' ) {
return prefix_insert_ads( $content );
}
return $content;
}
function prefix_insert_ads( $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
$paragraphs[$index] .= $closing_p;
if ( in_array($index, array(1, 5, 9)) ) {
//Replace the html here with a valid version of your ad code
$paragraphs[$index] .= '<p style="background:#f00">Ad goes here</p>';
}
}
return implode( '', $paragraphs );
}
Check functions in the section of Conditional Tags Index from https://codex.wordpress.org/Function_Reference
if(!is_page()) {
// do your tricks
}
There're also some other functions you may need like is_home(), is_front_page() and etc.
Related
I want to hide a certain paragraph from the SINGLE POST in WordPress via PHP content filter.
I developed this filtering script, which works like a sunshine:
function hideDaStuff( $content ) {
$paragraphs = explode( '</p>', $content );
foreach ($paragraphs as $index => $paragraph) {
if(6 == $index){
$paragraphs[$index] = '<p>Hidden for you ma friend.</p>';
}else{
$paragraphs[$index] .= '</p>';
}
}
return implode( '', $paragraphs );
}
add_filter( 'the_content', 'hideDaStuffForDaStranger' );
function hideDaStuffForDaStranger( $content ) {
if ( is_single() && ! is_user_logged_in() ) {
$content = hideDaStuff( $content );
}
return $content;
}
The problem is that WP cache caches the page whatever happens. So if a logged in user visits the page, then the content will show for everybody, and viceversa.
How can I make this specific part cache-independent, while keeping an efficient cache?
Using latest WP version and WP cache.
Thank you.
Put only single condition
function hideDaStuffForDaStranger( $content ) {
if (! is_user_logged_in() ) {
$content = hideDaStuff( $content );
}
return $content;
}
Actually no need of these filters. Use this format
if(is_user_logged_in()){ ?>
<p>oggedin data</p>
<?php }
I used this code to show responsive ad from adsense after 5th paragraph.
Adding Ads After First And Second Paragraph of WordPress Post
This is the code I use on my site:
function prefix_insert_after_paragraph2( $ads, $content ) {
if ( ! is_array( $ads ) ) {
return $content;
}
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
$n = $index + 1;
if ( isset( $ads[ $n ] ) ) {
$paragraphs[$index] .= $ads[ $n ];
}
}
return implode( '', $paragraphs );
}
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph2( array(
// The format is: '{PARAGRAPH_NUMBER}' => 'AD_CODE',
'5' => '<div>Ad code after FIRST paragraph goes here</div>',
), $content );
}
return $content;
}
I would like to exclude this code only for specific posts. How can I add the proper code to be able to exclude this function for post id=280?
Thank you.
Why do you want to exclude this function inside functions.php?
I think it's much easier to do it inside the post loop.
For example, to exclude the POST ID 280, if you're inside the page.php, or similar you can do this:
global $post;
if ($post->ID == 280){ remove_filter( 'the_content', 'prefix_insert_post_ads' ); }
This way you have the 'add_filter' on your functions.php file, and if you find the exception inside the post (ID=280), you remove the filter.
Just U have to check the current post_id. E.g. :
function prefix_insert_post_ads( $content ) {
global $post;
$post_id = $post->ID;
$post_ids_excluded = [280,....]; // excluded posts ids
if ( in_array($post_id,$post_ids_excluded) ){
return $content;
}
/*
... the same code .....
*/
}
I am using two different template for single page. One with sidebar and another one without sidebar.
Now i want to inject the ad code only in the sidebar template.
Could any one give me a code for this one.
Note:
The ad is automatically inserted after every second paragraph. And i have the code.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
if ( is_single() && ! is_admin()) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
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 );
}
The above code display the ad code both in sidebar template and no sidebar template. But actually i want to display the ad code only in sidebar template. Could any one please help me how to do it.
You can check which template is used with get_page_template_slug():
if ( is_single() && ! is_admin() && get_page_template_slug() == 'template-sidebar.php') {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
You can use this code on your single php file
if (is_page(12)) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
Thanks.
I have the following code that I'm using in my functions.php file to add a Google ad to the second paragraph of each post. As it stands, the code works but only on posts. I was hoping someone on this forum could assist with tweaking the code so this function is applied to pages as well to posts.
//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>AD Code to go here</div>';
if ( is_single() && ! 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 );
}
Instead of is_single(), which only tests whether a single posts are shown, you should use is_singular(), which test for is_single(), is_page() or is_attachment()
Here is a simple code which shows the Adsense or any ad code after second paragraph on single post page. But I want to know how to modify it so that it can run a dynamic function.
//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>Ads code goes here</div>';
if ( is_single() && ! 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 );
}
In fact I want to replace
$ad_code = '<div>Ads code goes here</div>';
with
$ad_code = '<?php if(function_exists('echo_ald_crp')) echo_ald_crp(); ?>';
Is there any way to do it??
Well, you can't assign PHP to a string var and expect it to execute. You could create a function with the code in it, have it return something, and then set the variable equal to the function call. Like this:
function output_function() {
//do something here
return $thing_you_want_to_return;
}
function prefix_insert_post_ads( $content ) {
$ad_code = output_function();
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
That way, when the variable is assigned, it'll run your function and return what you want to be echoed out with prefix_insert_after_paragraph().