Custom Web Applications to Grow Your Business

(256) 429-8145

How to automatically add Google ads in the center of your wordpress post using php codes

For a long time now I have been thinking about automatically adding ads at the center of every post of my wordpress blog. I just thought that by doing this one can have greater flexibility over blog or any web site ad placement. So, recent I gave it a go and created a solution that would allow the Google ads, banner ads or affiliate data feeds to dynamically pop up in the center of every post all the time. I used php codes (my favorite tool for building website) to dynamically cut the first section of the post and insert the ad (text or banner) at the bottom of it and then use php again to add the first section to the last section of the post with the ad sandwich between. This technique of adding ads in the center of the page, although a delicate one can be use in every kind of php dynamic page. Here is the solution I came up with: 1. Open php and convert get_the_content(); function into an accessible variable called $pcontent <?php $pcontent = get_the_content(); 2. Do string replace and apply filter to get rid of unwanted characters $pcontent = str_replace(']]>', ']]&gt;', $pcontent); $pcontent = apply_filters('the_content', $pcontent); 3. Create a sub string of your entire post under which your ad will be place. In this case I did a sub string with 700 characters. The sub string is assigned to variable $fsent $fsent = substr($pcontent, 0, 700); 4. Then create the sencond half of the post by completely remove the first half ($fsent) from the content $pcontent = str_replace(''.$fsent.'', '', $pcontent); 5. Then create a string that includes a few characters up to the first period (.) of the second string or $pcontent. I included (+1) so that the space after the period is removed. This is done to insure that there are no spaces at the beginning of the subsequent sentence. In addition, # 5 is done to ensure that the string is not created in the middle of a url, thus creating some serious problem with broken or damage links. $secndsent = substr($pcontent, 0, strpos($pcontent, ". ")+1); 6. Create a new variable called $lastsent by adding the first string ($fsent) and the broken sentence between the first and second strings ($secndsent) with concatenation symbols. This new variable is now the first part of your post above the ad. In this case I am using a data feed set up for random rotation from a table. $lastsent = $fsent.''.$secndsent.''; 7. Prepare the second half of you post by ensuring that the variable or string $secndsent is removed from it. $pcontent = str_replace(''.$secndsent.'', '', $pcontent); 8. Upon page view, print or echo the first part of the post or article echo $lastsent; Here is an example of the first part of the post. This is on my natural cure website with Native Remedies data feed sources from [caption id="attachment_105" align="alignnone" width="500" caption="First part of wordpress post with add below"]php affiliate data feed for word press[/caption] 9. Get data and print. Data can be from table, affiliate network, ad, data feed etc. As mention above, I am sourcing my ad from a data feed and have it displaying items in random order using the MysqlRAND() function. $result = mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 3"); if (!$result) { exit('<p> Error with cat'); } while($row = mysql_fetch_array($result)) { $product = $row['product']; $link = $row['link']; $img = $row['img']; $price = $row['price']; $desc = $row['desc']; $buy2 = $row['buy2']; $shipping = $row['shipping']; $warranty = $row['warranty']; echo' <div id = "productswrap">'; echo' <div id = "products">'; echo' <div id = "innerproducts">'; echo' <div id = "pmage">'; echo '<a rel="nofollow" href="'.$link.' " TARGET="_blank""> <img src ="'.$img.'" border = "0"></a>'; echo '</div>'; echo '<a rel="nofollow" href="'.$link. ' " TARGET="_blank"">' . $product. '</a><br>'; echo $desc .'<br>'; echo' <div id = "worldwide">'; echo $shipping .'<br>'; echo '</div>'; echo' <div id = "warranty">'; echo $warranty .'<br>'; echo '</div>'; echo '<a rel="nofollow" href="'.$link.'" TARGET="_blank""><img src="" border = "0"></a>'; echo '</center>'; echo '</div>'; echo '</div>'; echo '</div>'; } echo'<div style="clear:both;">'; echo'</div>'; 10. Print second have of the post echo $pcontent; Here is the second part of the post with 3 random products on display. [caption id="attachment_106" align="alignnone" width="500" caption="data feed in middle of wordpress post using php code"]datafeed in middle of wordpress post[/caption] 11. Close php ?> If this article is of help to you or you know of another php code that can accomplish the same or better result. Please drop me a line below

More Wordpress Hacks

WordCamp Detroit 2010 Wordpress Theme Hacks
View more presentations from John Pratt.