How to paste AdSense code into single post using Word Press

Want to know how to place Google AdSense unit to show adds on a single post of your WordPress site? Instead of using one more plugin I suggest you to try method I described.

This is an easy way to integrate Google AdSense code into a single post, but still – some basic code Editing steps are required. If you haven’t done anything like this before, maybe it is good thing to ask someone for help. In case you have done things like this before continue reading this article. I will explain how to integrate Google AdSense into a single Word Press post.
The best thing is that you don’t need to use Word Press plugin for this. Only slight code change is required to place AdSense into your post.
If you take a look at my blog you know what am I talking about. I choose to place Google AdSense code right after second paragraph. For me this is the best option, and if you decide to do this for your site you can simply change paragraph number.
To make changes first LogIn into your WordPress site with Administrator account. Navigate to Appearance > Editor and look for single.php file. In that file look for a line of code


<?php the_content(); ?>

which needs to be changed.

Notice: Before changing any code, first make sure to backup file you are going to edit. If something goes wrong simply re upload your original file and then look what went wrong.

Code shown above replace with this code

<?php
$paragraphAfter= 1;
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++) {
if ($i == $paragraphAfter) { ?>YOUR ADSENSE CODE GOES HERE<?php
}
echo $content[$i] . "</p>";
} ?>

save changes and see how it looks like on your site.

This code will place Google AdSense unit right after paragraph 1 and if you like to position AdSense unit on the other place just change $paragraphAfter=  ; line and that is it. Other thing you can do is to position your AdSense unit right, left or center. I made my AdSense unit to go left, and I choose mobile AdSense unit 320×100 which stay in the middle in case you are browsing my site by mobile device.
Important things to know: Not each WordPress Theme uses the same files like I described. Sometimes post content can be called through function from other file in which case you need to look for that file and search for <?php the_content(); ?> line of code. This is also important – If you are using Desktop WordPress Theme and mobile plugin keep in mind to change its own single.php. Browse your files and find single.php in plugin directory. Make changes like I described above to have AdSense even your visitors comes through mobile or tablet devices.
For the end I will place my <div> element so you can see how it goes left while you browsing my site on Desktop PC or to center if you are on your mobile device. Here is my code.


<div style=”display:block;float:left;margin: 5px 5px 5px 5px;”>

Just close that DIV after AdSense code and you will get your unit position to the left. Notice that I use margin:5px 5px 5px 5px; which make space in between AdSense unit and site content so I don’t violate Google AdSense TOS.

Add Comment