How to automatically insert AdSense ads in your WordPress posts


We're using AdSense on some of our websites and it is pretty easy to add the ads code to WordPress pages or posts.

When you write a post or page all you have to do is to paste the AdSense code exactly where you want. The ads will appear on your page or post.

Add AdSense code directly into your theme files

If you're tired to add the code all the time you have the option to add the code into your theme .php files.

You can add it before the content, after the content or to sidebar. If you open page.php or post.php you can easily find the_content() WP tag and paste AdSense code after or before it.

If you open sidebar.php, you can add the code there and so on. You get the idea.

How to add AdSense automatically to your WP posts and pages

But what if you want to add the AdSense ads automatically inside your post content?

This means without having to paste code every time you write a post.

Sounds cool, right? We have seen above that we can add this code before or after the_content() tag.

WordPress does not have any option to insert something inside the content.

That's true and therefore I've come with a solution for this. It is basically a PHP code which will allow you to automatically insert AdSense code after a certain number of paragraphs of a post.

The PHP code

Here's what you need to do:

1. Download your functions.php files to your computer. You need to connect via FTP to do this. This file it's found in your theme folder (wp-content/themes/your-theme-folder).

Open your functions.php file. Add the below code at the end of the file:

function zi_insertion_paragraph( $insertion, $paragraph_id, $content ) {
$close_p_tag = '</p>';
$paragraphs = explode( $close_p_tag, $content );
$paragraphs_no = count($paragraphs);
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $close_p_tag;
if (($paragraph_id > $paragraphs_no) && ($paragraphs_no == $index + 1)) {
$paragraphs[$index] .= $insertion;
} else if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;

return implode( '', $paragraphs );

function zi_insert_ad_code_after_paragraph( $content ) {
$ad_code = '<div>=====Ads code goes here=====</div>';

return zi_insertion_paragraph( $ad_code, 3, $content );

return $content;

add_filter( 'the_content', 'zi_insert_ad_code_after_paragraph' );

2. Upload functions.php file to your theme folder via FTP. The ads will appear after the 3rd paragraph in your post.

Note zi_insertion_paragraph( $ad_code, 3, $content ); - the second parameter has the value of 3. If you want the ads to appear after the 4th or 7h or whatever paragraph, you need to change the value of that parameter.

You don't need to worry about the number of paragraphs on your post or page. If the number of paragraphs on post or page it is smaller than the parameter value, the AdSense code will be added at the end of the content.

You need to add your AdSense code here: $ad_code = ‘<div>=====Ads code goes here=====</div>’;.

What AdSense ad types can I use with this code?

You can use any AdSense ad types. It is up to you. Just add the code to the variable and will appear on page.

Can I add other ads?

Yes, you can add other ads like, PropellerAds Media, Adsterra, Infolinks, PopAds, PopCash etc. Just add the code to the $ad_code variable.

Please let us know if this is working for you and if you have any questions we'll be happy to answer.

Comments closed

Please contact me, if you have any questions or suggestions.