Like the title says. I’ve successfully placed adsense ads on my AMP posts. While testing it out on my desktop via firebug, adverts are coming up as intended. However, I cannot get any ads to come up on my mobile? The pages body class changes to amp-mode-touch and the adverts div containers disappear completely.
Here is an example URL - http://www.gamersgreed.com/kingdom-come-deliverance-fps-boost-optimization/amp
And here is the code i'm using to produce the ads -
//Insert ads after third and seventh paragraph of any single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div style="text-align: center;"><amp-ad width=300 height=250
type="adsense"
data-ad-client="ca-pub-7568065840567088"
data-ad-slot="1234194857">
<div placeholder></div>
<div fallback></div>
</amp-ad>
</div>
';
$ad_code2 = $ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $ad_code, 2, $content );
$content = prefix_insert_after_paragraph( $ad_code2, 7, $content );
return $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 );
}
Related
I am trying to insert an advert before the first h2 tag in a post. The code I have tried is below, but the advert comes after the content and not before. I'm looking to insert the advert before the content, and not after.
/*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 = 'This is my ads';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
//Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</h2>';
$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 );
}
Any help would be appreciated.
Try this:
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '<h2>';
$paragraphs = explode( $closing_p, $content );
if ($paragraphs) {
$paragraphs[0] = $paragraphs[0].$insertion;
}
return implode( '<h2>', $paragraphs );
}
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'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.
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().