Categories
Wordpress Initial Setting

Adsense Auto-Ads with Related Contents Ad (via functions.php)

if you wish to add Adsense Auto-Ads and Related Contents Ads codes in functions.php, you can try the way that works for me.

【Last updated: April 2020】

Again, my recommendation is to use a plugin. But if you wish to add Adsense Auto-Ads and Related Contents Ads codes in functions.php, you can try the way that works for me.

Not exact translation but meaning of the code is;

If a visitor is viewing the AMP page, then show the AMP Auto-Ads; if a visitor is viewing the non-AMP page, then show normal Auto-Ads, if the admin (or authorised writer) is viewing the page, then do not show the ads.

Additional Header Script (HTML and AMP)

<?php function additional_head_script () {
$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'amp') !== false && !is_user_logged_in()): ?>
<script async custom-element="amp-auto-ads"
src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
</script>
<?php elseif (strpos($url,'amp') == false && !is_user_logged_in()): ?>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-XXXXXXXXXXXXXXXX",
enable_page_level_ads: true
});
</script>
<?php endif; 
}
add_action( 'wp_head', 'additional_head_script' );
?>

AMP-Auto Ads, Top of the Body Script

<?php function amp_additional_body_open_script () {
$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'amp') !== false && !is_user_logged_in() ): ?>
<amp-auto-ads type="adsense"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX">
</amp-auto-ads>
<?php endif;
}
add_action( 'wp_body_open', 'amp_additional_body_open_script' ); ?>

Related Ads after Contents

<?php function related_ad($content) {
$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'amp') !== false && !is_user_logged_in()) {
$aftercontentamp = '<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>
<amp-ad width="100vw" height="320"
type="adsense"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
data-auto-format="mcrspv"
data-full-width="">
<div overflow=""></div>
</amp-ad>';
$fullcontent = $content . $aftercontentamp;
} elseif (strpos($url,'amp') == false && !is_user_logged_in()) {
$aftercontent = '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block"
data-ad-format="autorelaxed"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
$fullcontent = $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'related_ad');
?>

Reference: wpdeveloppers, Adding Content Before and After the_content!

Should you wish to know the code without Related Contents Ads, then please refer;

Thanks!

Any advice will be highly appreciated!

Hope this information helps.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.