Created
February 10, 2017 22:35
-
-
Save mwordpress/19af9d9dc99fe7041b2f807d8ec2294a to your computer and use it in GitHub Desktop.
the codes listed here is part of the following article : https://www.mwordpress.net/how-to-add-adsense-into-post-wordpress/
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 | |
$content = apply_filters('the_content', $post->post_content); | |
$save = explode("</p>", $content); | |
$tcount = 0; | |
$adon = 0; | |
foreach($save as $item) { | |
echo $item; | |
echo "</p>"; | |
if(preg_match('/<p> /',$item) == 0 && $tcount >= 1 && $adon == 0) { | |
$adon=1; | |
?><div class="ad-code">هنا يمكن وضع اعلانات جوجل ادسنس</div><?php | |
} | |
if(preg_match('/<p> /',$item)==0 && $tcount>=4 && $adon==1) { | |
$adon=2; | |
?><div class="ad-code">هنا يمكن وضع اعلانات جوجل ادسنس</div><?php | |
} | |
$tcount++; | |
} | |
?> |
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
//Insert ads after second paragraph of single post content. | |
add_filter( 'the_content', 'prefix_insert_post_ads' ); | |
function prefix_insert_post_ads( $content ) { | |
$paragraph = get_post_meta($post->ID, 'mw_paragraph' , true); // by mwordpress.net | |
$hide = get_post_meta($post->ID, 'mw_hide_ads' , true); // by mwordpress.net | |
if (!$hide) : | |
$ad_code = ads_code_print(); | |
endif; | |
if ( is_single() && ! is_admin() ) { | |
if (!$paragraph) { | |
return prefix_insert_after_paragraph( $ad_code, 2, $content ); | |
} else { | |
return prefix_insert_after_paragraph( $ad_code, $paragraph, $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 ); | |
} | |
function print_ads_code() { | |
echo '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<ins class="adsbygoogle" | |
style="display:block" | |
data-ad-client="ca-pub-XXXXXXXXXXXXXXX" | |
data-ad-slot="XXXXXXXXXX" | |
data-ad-format="auto"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script>'; | |
} |
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 the_content(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment