How to Insert AdSense Ad within Post Content in WordPress (without Plugin)

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

Best place for placing AdSense ad is within the content. By doing so, we increase the visibility of ad, and hence there are better chances of readers checking them.

Inserting Ads within post content in a WordPress site isn’t easy since we need to know the programming structure of WordPress or use a plugin to do this. But in today’s post, we’ll do this using a simple code that you can copy and paste.

1. Displaying ad to left/right of post content, in first paragraph

1. Open post-template.php file contained in ‘wp-includes’ folder of your WordPress installation.

open post-template.php file in wp-includes of wordpress installation

2. Inside post-template.php, find the the_content function which will be somewhere in the 211th line of code.

3. In the code below, add your AdSense code in the $ad variable. Copy and paste the code after get_the_content function inside the_content function.

<?PHP
if( is_single() ) { // If posts, not for pages, homepage or any other page
$ad = "<p style='margin:15px;float:right;'>"; // Float right or left depending on the ad position you want
$ad .= "//------YOUR ADSENSE CODE------//"; // Place your AdSense code within quotes
$ad .= "</p>";
$content = $ad.$content; // Append AdSense code ad to the content
}
?>

4. Save the file. The the_content function should look something like this.

add adsense code within post content

5. That’s it. Check your website, a small block of ad would be appearing alongside the text.

2. Displaying ad in 2nd or 3rd paragraph, towards left or right

This too isn’t tough, all you need to do is to break content into two parts and put AdSense code sandwiching between the two. Here is how you can do this,

<?PHP
if( is_single() ) {
$new_content_part1 = $new_content_part2 = '';

$content_split_by_space = explode( ' ', $content ); // Break content from space

for( $i = 0; $i < 100; $i++ ) {
$new_content_part1 .= $content_split_by_space[$i].' '; // First to 100th word
}

$ad = "<p style='margin:15px;float:right;'>";
$ad .= "//------YOUR ADSENSE CODE------//";
$ad .= "</p>";

for( $i = 100; $i < count( $content_split_by_space ); $i++ ) {
$new_content_part2 .= $content_split_by_space[$i].' '; // 100th word to Last
}

$content = $new_content_part1.$ad.$new_content_part2; // Ad between two parts
}
?>

This is one way of doing this. You can break content from full stop(sentence end), comma or whatever you prefer. Another important to remember is that you should keep smaller sized ads in the middle as, sometimes, big ads deteriorates the reading experience of the visitors which shouldn’t happen.

If your ad isn’t appearing correctly, specify the height and width of the ad container ie the element <p> in which you’re putting the code.

If there is any problem, or there is any better way of doing this, please mention in comments.

blog comments powered by Disqus