Last active
October 30, 2019 06:39
-
-
Save Balachandark/1850c565905a6b1fa4e05abb485a9d37 to your computer and use it in GitHub Desktop.
Add content before, after content and in between paragraphs
This file contains 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
//Insert ads after fifth and seventh paragraph of single post content. | |
add_filter( 'the_content', 'prefix_insert_post_ads' ); | |
function prefix_insert_post_ads( $content ) { | |
// Add your after 5th paragraph ad code first and then the 7th paragraph ads code. | |
$ad_code = array( | |
'<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- Mypt3 300x250 (M) --> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-8871976336473556" | |
data-ad-slot="8933279338" | |
data-ad-format="auto" | |
data-full-width-responsive="true"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script>', | |
'<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- Mypt3 300x250 (D) --> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-8871976336473556" | |
data-ad-slot="9681670287"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script>' | |
); | |
if ( is_single() && ! is_admin() ) { | |
/* Change the paragraph number as per your preference, currently with the following code the Ads code can be added after the second and fifth paragraph */ | |
return prefix_insert_after_paragraph( $ad_code, array( 5, 7 ) , $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; | |
} | |
$i = 0; | |
foreach( $paragraph_id as $id ){ | |
if ( $id == $index + 1 ) { | |
$paragraphs[$index] .= $insertion[$i]; | |
} | |
$i++; | |
} | |
} | |
return implode( '', $paragraphs ); | |
} | |
add_action( 'astra_entry_content_before', 'add_ads_before_content' ); | |
function add_ads_before_content() { | |
if ( ! is_single() ) { | |
return; | |
} | |
// Add your before content ads code here. | |
echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- Mypt3 300x250 (M) --> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-8871976336473556" | |
data-ad-slot="8933279338" | |
data-ad-format="auto" | |
data-full-width-responsive="true"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script>'; | |
} | |
add_action( 'astra_entry_content_after', 'add_ads_after_content' ); | |
function add_ads_after_content() { | |
if ( ! is_single() ) { | |
return; | |
} | |
// Add your after content ads code here. | |
echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- Mypt3 300x250 (D) --> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-8871976336473556" | |
data-ad-slot="9681670287"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment