Created
September 19, 2016 15:01
-
-
Save isaumya/7b83f3653f195fa6aeaf2f1c7b32accd to your computer and use it in GitHub Desktop.
Insert AdSense Ad Code after certain number of paragraphs in your WordPress posts dynamically while ignoring some paragraphs in the paragraph checking/count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Insert AdSense Ad Code after certain paragraph | |
Plugin URI: https://www.isaumya.com/ | |
Description: Insert AdSense Ad code after certain paragraph number while ignoring some paragraphs | |
Version: 2.0 | |
Author: Saumya Majumder | |
Author URI: https://www.isaumya.com/ | |
*/ | |
/*Insert Adsense Ad Code in the post page*/ | |
add_filter('the_content', function( $content ) { | |
global $post; | |
if ($post->post_type == 'post') { | |
// Setting up the the HTML Charecter encoding to UTF-8 | |
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'); | |
// Starting the main XML creation | |
libxml_use_internal_errors(true); | |
// Initializing a new DOM object | |
$dom = new DOMDocument; | |
// Load HTML content | |
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); | |
// Initializing a new XPath object | |
$xpath = new DOMXPath($dom); | |
// Query all `p` tags that their parent is not those specific elements | |
/* List of `p` tags that the Query will not take into consideration: | |
* `p` tags which has class name => some_class_1 e.g. <p class="some_class_1">...</p> | |
* `p` tags which has a `div` within it with following classes: some_callout, some_notice, some_other_class | |
* `p` tags which has `pre` tags within it | |
* `p` tags which has `code` tags within it | |
* `p` tags which has `blockquote` tags within it | |
* `p` tags which is under a `div` with class some_parent_class | |
* `p` tags which has only an anchor tags and image tag within it; e.g. <p><a href="some link"><img src="some image url" /></a></p> | |
**/ | |
$paragraphs = $xpath->query('//p[not(contains(@class, \'some_class_1\') or ancestor::div[contains(@class, \'some_callout\') or contains(@class, \'some_notice\') or contains(@class, \'some_other_class\')] or ancestor::pre or ancestor::code or ancestor::blockquote or descendant::div[contains(@class, \'some_parent_class\')] or a/img )]'); | |
// Let's intert the ad code after 1st paragraph | |
// Make sure there is at least 3 or more paragraphs present before | |
// pushing the ad after 1st paragraph | |
if ( $paragraphs->length > 2 ) { | |
// Set the paragraphNumber after which the ad will me inserted | |
$paragraphNumber = 1; | |
// Declaring the ad code for showing after 1st paragraph | |
/* --------------------------------------------------------------- */ | |
// Please make sure your ad code in enclosed within <div></div> tags | |
// if you want you may add some class to this outer div that hold the | |
// ad code like this <div class="some_class">...Your ad code goes here...</div> | |
// Without the <div></div> enclosement the ad code will not show up and you may face | |
// broken page content issue | |
/* --------------------------------------------------------------- */ | |
// Setting up the default ad code value that will show up for normal webpages | |
$adCode = '<div class="some_ad_class"> | |
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- Some Responsive Ad Code --> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-XXXXXXXXXXXXXX" | |
data-ad-slot="XXXXXXXXXXXXXX"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script> | |
</div>'; | |
// Check if the site as any AMP enabled pages which is being created by the | |
// official WordPress AMP plugin: https://wordpress.org/plugins/amp/ | |
if ( function_exists( 'is_amp_endpoint' ) ) { | |
// If the user is viewing an AMP page of any article, push the AMP version of | |
// AdSense ad code | |
if ( is_amp_endpoint() ) { | |
$adCode = '<div class="some_amp_ad_class"> | |
<!-- AMP Ad Code --> | |
<amp-ad width=300 height=250 | |
type="adsense" | |
data-ad-client="ca-pub-XXXXXXXXXXXXXX" | |
data-ad-slot="XXXXXXXXXXXXXX"></amp-ad> | |
</div>'; | |
} | |
} // end if of checking the AMP endpoint | |
$newDom = new DOMDocument; | |
$newDom->loadHTML($adCode, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); | |
$node = $newDom->getElementsByTagName('div')->item(0); | |
$adNode = $dom->importNode($node, true); | |
$paragraphs->item($paragraphNumber)->parentNode->insertBefore($adNode, $paragraphs->item($paragraphNumber)); | |
} // end if for adding ad code after 1st paragraph | |
/*---------------------------------------------------*/ | |
// Let's intert the ad code after 9th paragraph | |
// Make sure there is at least 12 or more paragraphs present before | |
// pushing the ad after 9th paragraph | |
if ( $paragraphs->length > 11 ) { | |
// Set the paragraphNumber after which the ad will me inserted | |
$paragraphNumber = 9; | |
// Declaring the ad code for showing after 9th paragraph | |
/* --------------------------------------------------------------- */ | |
// Please make sure your ad code in enclosed within <div></div> tags | |
// if you want you may add some class to this outer div that hold the | |
// ad code like this <div class="some_class">...Your ad code goes here...</div> | |
// Without the <div></div> enclosement the ad code will not show up and you may face | |
// broken page content issue | |
/* --------------------------------------------------------------- */ | |
// Setting up the default ad code value that will show up for normal webpages | |
$adCode = '<div class="some_ad_class"> | |
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- Some Responsive Ad Code --> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-XXXXXXXXXXXXXX" | |
data-ad-slot="XXXXXXXXXXXXXX"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script> | |
</div>'; | |
// Check if the site as any AMP enabled pages which is being created by the | |
// official WordPress AMP plugin: https://wordpress.org/plugins/amp/ | |
if ( function_exists( 'is_amp_endpoint' ) ) { | |
// If the user is viewing an AMP page of any article, push the AMP version of | |
// AdSense ad code | |
if ( is_amp_endpoint() ) { | |
$adCode = '<div class="some_amp_ad_class"> | |
<!-- AMP Ad Code --> | |
<amp-ad width=300 height=250 | |
type="adsense" | |
data-ad-client="ca-pub-XXXXXXXXXXXXXX" | |
data-ad-slot="XXXXXXXXXXXXXX"></amp-ad> | |
</div>'; | |
} | |
} // end if of checking the AMP endpoint | |
$newDom = new DOMDocument; | |
$newDom->loadHTML($adCode, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); | |
$node = $newDom->getElementsByTagName('div')->item(0); | |
$adNode = $dom->importNode($node, true); | |
$paragraphs->item($paragraphNumber)->parentNode->insertBefore($adNode, $paragraphs->item($paragraphNumber)); | |
} // end if for adding ad code after 9th paragraph | |
/*---------------------------------------------------*/ | |
// Let's intert the ad code after 19th paragraph | |
// Make sure there is at least 21 or more paragraphs present before | |
// pushing the ad after 19th paragraph | |
if ( $paragraphs->length > 21 ) { | |
// Set the paragraphNumber after which the ad will me inserted | |
$paragraphNumber = 19; | |
// Declaring the ad code for showing after 19th paragraph | |
/* --------------------------------------------------------------- */ | |
// Please make sure your ad code in enclosed within <div></div> tags | |
// if you want you may add some class to this outer div that hold the | |
// ad code like this <div class="some_class">...Your ad code goes here...</div> | |
// Without the <div></div> enclosement the ad code will not show up and you may face | |
// broken page content issue | |
/* --------------------------------------------------------------- */ | |
// Setting up the default ad code value that will show up for normal webpages | |
$adCode = '<div class="some_ad_class"> | |
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- Some Responsive Ad Code --> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-XXXXXXXXXXXXXX" | |
data-ad-slot="XXXXXXXXXXXXXX"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script> | |
</div>'; | |
// Check if the site as any AMP enabled pages which is being created by the | |
// official WordPress AMP plugin: https://wordpress.org/plugins/amp/ | |
if ( function_exists( 'is_amp_endpoint' ) ) { | |
// If the user is viewing an AMP page of any article, push the AMP version of | |
// AdSense ad code | |
if ( is_amp_endpoint() ) { | |
$adCode = '<div class="some_amp_ad_class"> | |
<!-- AMP Ad Code --> | |
<amp-ad width=300 height=250 | |
type="adsense" | |
data-ad-client="ca-pub-XXXXXXXXXXXXXX" | |
data-ad-slot="XXXXXXXXXXXXXX"></amp-ad> | |
</div>'; | |
} | |
} // end if of checking the AMP endpoint | |
$newDom = new DOMDocument; | |
$newDom->loadHTML($adCode, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); | |
$node = $newDom->getElementsByTagName('div')->item(0); | |
$adNode = $dom->importNode($node, true); | |
$paragraphs->item($paragraphNumber)->parentNode->insertBefore($adNode, $paragraphs->item($paragraphNumber)); | |
} // end if for adding ad code after 19th paragraph | |
libxml_use_internal_errors(false); | |
// Finally save every changes | |
$content = $dom->saveHTML(); | |
} // end if of checking the post type == post | |
return $content; //return the content back to WordPress | |
}, 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment