Monday, December 30, 2019

AMP Posts / Pages Me Adsense Ads Insert Kaise Kare [Without Plugin]

AMP Posts / Pages Me Adsense Ads Insert Kaise Kare [Without Plugin]

Today we are going to tell you about inserting Adsense Ads in AMP posts / pages. If you also use AMP in your wordpress blog, then this post can be very helpful for you. We will tell you how to insert ads without using any plugin.
insert ads into amp post and pages without plugin
When we talk about blogging, ads are a very major thing for all bloggers. Everyone writes hard posts in their blog so that they get some benefit from it. Every blogger earns money by showing their blog advertising or through affiliate marketing.
Most people use Adsense to display advertising in their blogs. So today we will tell you about showing adsense ads in AMP pages.
Many people have asked me till now how have you inserted ads in AMP pages? Through the comment, I was able to tell them that “I have done manually coding” so today I am going to tell you with people how to show ads without plugins in AMP posts & pages?
I use AMP for a long time in my blog. This has given me a very positive result. Since amp is recognized in Google. That's why Google always pays attention to amp pages for mobile devices.
If you do not know about it, then I have already written a post about it, you can read it. Also, if you have to setup the amp in your blog, then you can read this post.
I use the “AMP for WP” plugin in my blog. By reading the above post, you can also setup it in your blog.
Currently, I am going to tell you how to show ads in AMP post & pages? if you “AMP for WPIf you use the plugin, it will work for you only. If you use another plugin for the amp, it may not work for you. (We have not tested)
So, we are going to tell you a very simple process for this. You do not have to use any kind of plugin for this. Just you have to add some codes to your theme.
There are many methods for this. You can also insert through the plugin. But I am telling you the best way. For this, you do not have to edit the coding of the plugin. Due to which you have the problem of losing the code after the plugin is updated.
To show adsense ads in AMP is much different than normal pages. So, let me first explain about it to you.
First of all, you have to log into Adsense and create different types of ads. You have to copy publisher id and ad slot from Ad code. For example, you can see below.

Above is adsense publisher id in green color and ad slot in red color. The publisher id of all ads will be the same, you just have to note the ad slot. Now, we are telling you about inserting ads.
Now you have to modify the code below and add it to the functions.php of your blog theme. Let us know how to modify it.
/ **
* Add Google Adsense code to AMP
* /
add_action ('pre_amp_render_post', 'isa_amp_add_content_filter_alt');
function isa_amp_add_content_filter_alt () {
add_filter ('the_content', 'isa_amp_adsense_within_multiple');
}
function isa_amp_adsense_within_multiple ($ content) {
$ ad_units = array (
1 => 'XXXXXXXXXX',
10 => 'XXXXXXXXXX',
20 => 'XXXXXXXXXX',
30 => 'XXXXXXXXXX',
);
$ insertions = array ();
foreach ($ ad_units as $ paragraph => $ ad_slot) {
$ insertions[ $paragraph ] = '';
}
// Insert Adsense ad after multiple paragraphs
$ new_content = isa_insert_after_paragraph_multiple ($ insertions, $ content);
return $ new_content;
}
function isa_insert_after_paragraph_multiple ($ insertions = array (), $ content) {
$ closing_p = '';
$ paragraphs = explode ($ closing_p, $ content);
foreach ($ paragraphs as $ index => $ paragraph) {
if (trim ($ paragraph)) {
$ paragraphs[$index] . = $ closing_p;
}
// insert ad at multiple places
foreach ($ insertions as $ paragraph_id => $ ad_code) {
if ($ paragraph_id === $ index + 1) {
$ paragraphs[$index] . = $ ad_code;
}
}
}
return implode ('', $ paragraphs);
}
The paragraph in which to insert ads blue color Can replace with number of We have inserted ads in 1, 10, 20, 30 paragraphs. You can adjust it according to your own. Red color (XXXXXXXXXX) Different ads in place of ad slot Have to replace. Other than that ca-pub-xxxxxxxxxxxxxxxxx Instead of adding your AdSense publisher id.
Now the ads have been inserted in your blog but the ads will not show yet. The code given to you under your theme functions.php Must be added to Only then will your blog ads show.
/ **
* Add the AMP Ad script to AMP pages.
* /
function isa_load_amp_adsense_script ($ data) {
$ data['amp_component_scripts']['amp-ad']    = 'https://cdn.ampproject.org/v0/amp-ad-0.1.js';
return $ data;
}
add_filter ('amp_post_template_data', 'isa_load_amp_adsense_script');
Now your blog will start showing ads. In this way you can show aids in your amp posts and pages very easily. Its best thing is that whenever you update the plugin, the ads code will not be removed. Because we have added the codes to our theme's functions.php.
Hope you have liked this post. If you have any questions related to this, then you can comment below. If you liked the post, then share it in social media. Join the blog's newsletter for the latest updates.

No comments:

Post a Comment