A web development/programming blog providing info, tips, and tricks on programming languages, scripting, Linux, MySQL and more
How to Insert Google Ads into the Content of your WordPress Post
Have you ever wondered how to get your Google Ads inside your post content like I have below? A couple of days ago, I was trying to figure out how to insert Google Adsense Ads directly into my post content without pasting the whole Adsense code. I came across an article that detailed exactly what I was looking for: Insert Google Adsense Code Anywhere In Your Wordpress Article.
Phillip made a very easy function (posted below) that allows you to insert code for multiple Adsense units into your blog theme’s functions.php file and then call it with the comment-style tag <!–Adsense1–> (a la <!–more–>). After modifying the functions.php file, you only need to modify the single.php file to call the function in order to get return the Ads.
The beauty of the function is not only that you can create additional adsense units and call which specific one you need, but that if you happen to place your <!–Adsense1–> code before the <!–more–> tag or within the post excerpt, it will be read as a regular HTML comment since the index.php or archive.php files do not recognize <!–Adsense1–> as a WordPress tag.
Check back on my blog or subscribe to my feed as I plan on turning this function into a WordPress plugin to streamline the installation and use even more.
——————
Edit your theme’s functions.php file by appending the following lines to the end of the file. Your functions.php file is located at /wp-content/themes/[your theme name]/functions.php. If you do not have a functions.php file, you can create one with this code (make sure you replace your Adsense Code)
function get_the_content_with_formatting ($more_link_text='', $stripteaser=0, $more_file='')
{
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
function AddGoogleAds ($content)
{
$Adsense1_1 = <<<ADSCODE1
<script type="text/javascript"><!--
google_ad_client = "pub-9999999999999999";
google_ad_slot = "0000000000";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
ADSCODE1;
$Adsense2 = <<<ADSCODE2
<script type="text/javascript"><!--
google_ad_client = "pub-9999999999999999";
google_ad_slot = "1111111111";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>ADSCODE2;
$content = str_replace(array('<p><!--Adsense1--></p>','<!--Adsense1-->'),$Adsense1,$content);
$content = str_replace(array('<p><!--Adsense2--></p>','<!--Adsense2-->'),$Adsense2,$content);
return $content;
}
After you have made the above changes, make the following changes to your single.php file, again in your theme’s directory. Find the line with the tag the_content(parameters) and replace it with the content below:
<?php
$cont = get_the_content_with_formatting();
$cont = AddGoogleAds($cont);
echo $cont ;
?>
<?php /* the_content('(more)'); */ ?>
Again, to insert your ads into your post content, place the tag <!–Adsense1–> or <!–Adsense2–> into your content. You can continue adding more Adsense units to your functions.php file following the same templaye as what is currently there.

