Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
25 Million+
Websites using our plugins
Years of WordPress experience
WordPress tutorials
by experts

How to Insert Ads Within Your Post Content in WordPress

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Do you want to insert ads within your post content in WordPress?

Your post content is the place where visitors are most engaged with your website, which makes it the perfect place to show an ad.

In this article, we will show three ways to insert ads within your post content in WordPress.

How to insert ads within your post content in WordPress

Why Insert Ads Within Your Post Content in WordPress?

Banner and sidebar ads are a great way to make money online blogging with WordPress. However, they’re also easy for visitors to ignore. 

People visit your site to read your content, and may not pay as much attention to your sidebar or other areas of the page. Since your post content gets the most attention from visitors, it’s the perfect place to show ads.

With that being said, let’s look at three different ways to insert ads within your post content in WordPress.

If you prefer to jump straight to a particular method then you can use the links below.

Method 1. Inserting Ads Within Post Content using WPCode (Automatically)

One way to insert ads within your post content is by using the free WPCode plugin.

WPCode is one of the best WordPress code snippets plugin on the market and it doubles as one of the top WordPress ad management plugins. It allows you to easily insert code to place ads on any page, post, or widget area of your WordPress website.

You can show advertisements from third-party ad networks such as Google AdSense. Or, you can display your own hosted ads, which is perfect for selling ads on your WordPress blog.

To get started, you’ll need to install and activate the WPCode plugin. If you need help, then please see our guide on how to install a WordPress plugin.

Upon activation, go to Code Snippets » + Add Snippet from your WordPress dashboard.

This will bring you to the built-in code snippets library. Hover over the ‘Add Your Custom Code (New Snippet)’ option, and click the ‘Use snippet’ button to select it.

Choose the Custom Code option in the WPCode library to insert an ad

Next, you’ll need to give your ad a title by typing into the field that shows ‘Add title for snippet.’

Your site visitors won’t see the title since it’s for your own reference only.

Add title for your ad code snippet in WPCode

Next, you need to choose the type of ad you want to place on your site.

One option is to use the ad code provided by a program like Google AdSense.

If you’re using Google AdSense code, then you may want to see our guide on the highest performing Google AdSense banner sizes and formats. We also have a complete guide on how to optimize your AdSense revenue in WordPress.

Another option is to create your own ad. In that case, you would need to get the code from the person who is paying you to show the ad on your WordPress site or write the code yourself.

Once you have the ad code, simply copy and paste it into the ‘Code Preview’ box.

You also need to select ‘HTML Snippet’ as the ‘Code Type’ from the dropdown list on the right side of the screen.

Copy and paste ad code into Code Preview box

After that, scroll down to the ‘Insertion’ section. There are two main insertion options you can choose from:

  • Auto-Insert – The ad code will be automatically inserted and executed on your website in the location that you choose.
  • Shortcode – The ad code will not be automatically inserted. You will get a shortcode that you can manually insert anywhere on your WordPress blog.

Click on the ‘Auto-Insert’ method to automatically display ads within post content.

Then, choose the ‘Insert After Paragraph’ option from the dropdown list next to the ‘Location’ field.

Select Auto-Insert Insert After Paragraph to insert ad within post content

You can also modify the ‘Insert Number’ to choose the number of paragraphs after which to insert the snippet. For example, if you want to display the ad after the second paragraph instead of the first, simply replace the 1 with a 2.

Aside from inserting ads after a paragraph, you can also automatically display ads in locations (WordPress functions) such as before or after content, between posts, before or after excerpts, and more.

Additional auto-insert locations in WPCode

With the premium version of WPCode, you can also insert ads in WooCommerce locations like before or after products, cart, and checkout page, just to name a few.

WPCode Pro also lets you schedule your ad code snippets with a start and end date. This is perfect for displaying ads around the holiday season or for other limited-time promotions.

Next, you can scroll down to the ‘Device Type’ section. Here, you can choose whether you want to display your ad on any device type, desktop only, or mobile only.

Choose which device types you want to display the ad on using WPCode

If you want to limit the pages or posts where the ad code snippet is auto-inserted, you can scroll down to the ‘Smart Conditional Logic’ section.

For example, you can hide your ads on specific page types or page URLs.

Use WPCode Conditional to limit pages where the ad appears Logic to

To help organize your ads, you can go to the ‘Basic info’ section. In this area, you can add tags and notes to help you remember what your ad code snippet is for.

Add tags to organize your ad code

There’s also a ‘Priority’ field that allows you to control the order in which the snippets are executed when you want to display multiple snippets in the same location.

All snippets get a priority of 10 as a default, so if you want your ad code to display earlier than others, simply set the priority to a lower number.

Once you’ve finished working on your ad, it’s time to publish it.

To do that, go back to the top of the page and toggle the switch from ‘Inactive’ to ‘Active.’ Then, click on the ‘Save Snippet’ button.

Activate and save ad code in WPCode plugin

That’s it! Your ad will now automatically be inserted within your post content if you selected the ‘Auto-Insert’ method shown in this tutorial.

If you chose the ‘Shortcode’ insertion method instead, you can either create a new page or post, or open one for editing. Then, simply find the exact place where you want to show this ad within your post content, and create a new shortcode block.

You can then go ahead and paste the shortcode into this block. For step by step instructions, see our beginner’s guide on how to add a shortcode in WordPress.

Now, you can visit the page or post to see the ad within your post content.

Note: If you are displaying Google AdSense ads, it may take 20-30 minutes for the ad to appear on the page.

Method 2. Inserting Ads Within Post Content using Ad Inserter

Another option is to use Ad Inserter. This is a WordPress ad management plugin that can show both hosted ads and advertisements provided by a network.

If you’re using a network then you can simply paste your ad code into the Ad Inserter plugin. However, if you’re creating a hosted ad, then you’ll either have to write the code yourself, or get it from the person who is paying to show their ad on your website.

The first thing you need to do is install and activate the free Ad Inserter plugin. If you need help, then please see our guide on how to install a WordPress plugin.

Upon activation, go to Settings » Ad Inserter.

The Ad Inserter WordPress plugin

On this screen, each tab is a different advertisement. This allows you to create multiple different ads, and place them on your website.

You can now either type in or copy/paste the code for the advertisement that you want to show within your post content.

Insert ads within your post content in WordPress using Ad Inserter

The next step is placing this ad on your website. One option is to add the advertisement to any page, post, or widget-ready area using a shortcode.

Another option is to tell Ad Inserter to automatically add this advertisement to every post, at a certain location such as after every third paragraph.

If you want to add this advertisement to your posts automatically, then start by checking the ‘Post’ box.

The Ad Inserter advert management plugin

Next, you’ll need to open the ‘Insertion’ dropdown and choose where Ad Inserter should show this ad.

Since we want to insert ads within the post content, you’ll typically want to choose either Before Paragraph, After Paragraph, Before Image, or After Image.

Placing the ad within your WordPress post content

After choosing an option from the list, type in exactly where this ad should appear.

For example if you wanted to show the ad after the second paragraph, then you would type in ‘2’ as you can see in the following image.

Inserting ads within your post content in WordPress

When you’re happy with your settings, click on the ‘Save Settings’ button.

Ad Inserter will now insert this ad within your post content, in your chosen location.

Saving your ad settings

Just like we mentioned earlier, you can also place this ad on your site using a shortcode.

To do this, click on the ‘Manual’ button and then check the ‘Shortcode’ box.

Adding an ad to your post content using shortcode

Once you’ve done that, just copy the Ad Inserter shortcode. You can then click on the ‘Save Settings’ button to save your ad.

With that done, you’re ready to place this shortcode on any page, post, or widget-ready area. For step by step instructions, you can see our guide on how to add a shortcode in WordPress.

Method 3. Inserting Ads Within Post Content using Code (Advanced)

You can also add an advertisement to your post content in WordPress using code.

Since you’re editing your site’s code, this isn’t the most beginner friendly method. However, it does allow you to create ads without having to install a special ad management plugin.

To insert an ad using code, either open your theme’s functions.php file, create a site-specific plugin, or use a code snippets plugin.

Again, we recommend using the free WPCode plugin because it makes it easy to paste snippets into WordPress, without editing your theme’s functions.php file.

Plus, WPCode comes with smart code snippet validation to help you prevent common code errors and protect you from breaking your site.

You can then add the following code:

//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
    $ad_code = '<div>Ads code goes here</div>';
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    return $content;
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
    return implode( '', $paragraphs );

This code will show an advertisement after the second paragraph of a post’s content.

If you want to show your ad in some other location, then you simply need to change the following section:

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 3, $content );

In the sample code above, you can see that we’re inserting the ad after the third paragraph.

Once you’ve done that, you can go ahead and either type in or paste your ad code. This is the code that controls how your ad looks and acts.

You’ll add this code as the $ad_code value, which is where it says ‘Add code goes here.’

    $ad_code = '<div>Ads code goes here</div>';

After you’ve done that, you’re ready to publish your ad. To do this, either activate your site-specific plugin, save the changes you’ve made to your site’s functions.php file, or activate and save your code snippet in WPCode.

Now if you visit your website, you should see your ad live.

We hope this article helped you learn how to insert ads within your post content in WordPress. You may also want to learn how to increase your blog traffic, or check out our list of the best WordPress tools and SEO plugins that you should use.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

215 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Manuel Graf says

    Thanks, it works for me, but I need a feature, can the magic only happen if there are more than 1500 characters in the Post? How can I do this?
    Thanks a lot!

    • WPBeginner Support says

      We do not have a recommended method for only activating if there are a certain number of characters but if we do we will look to update the article :)


  3. Erika says

    Will the Ad Sanity allow to place external ads on an individual post only not on the entire site?

  4. George says

    Is there any way how to insert different ads after multiple paragraphs? For example AdSense ad no 1 after 2nd paragraph, AdSense no 2 ad after 6th paragraph?

    • WPBeginner Support says

      If your ad network has different ads to display then method 2 should allow for what you are wanting currently :)


  5. Gideon says

    will this code replace the entire existing code of the theme child theme?

    The <?php start is returning error

    • WPBeginner Support says

      It will not replace your entire theme’s design. For pasting the code in your functions.php you could try removing that opening PHP tag to see if that helps solve the issue.


  6. Oniel says

    Can you please update the plugin to be able to add the code “before” the first paragraph instead of “after” the first paragraph?

    • WPBeginner Support says

      The plugin does have the option to insert before your post content instead of after a paragraph number


  7. Shannon says

    This is amazing, thank you so much for this. Do you know why my ad goes to the left of the post and not in the middle?

    • WPBeginner Support says

      The most common reason would be if your theme has styling that is forcing that alignment.


  8. Karthik says

    how much time take for adsense ad display on our site once we get approved after the code placed.

    • WPBeginner Support says

      That would be a question for Adsense as they may not have ads they would currently offer for your site.


  9. Sangam says

    Thank you, with the help of this snippet I managed to show ad after each 300 words. I just added word count instead of paragraph.

  10. Chango says

    Hello is that Insert posts ads plug in good? or does it crash the site or slow it down? I am nervous because 7 years ago I used a adsense plug in and the maker of the plug in was adding their ads on my website.

    Is this insert ads plug in good that you recommend. Does it mess with coding or slow down the site?


    • WPBeginner Support says

      Unless you manually add problematic code, the insert post ads plugin shouldn’t harm your site’s speed.


  11. Arkantos says

    Works great! But I noticed it also counts the paragraphs from an embedded tweet. Is it a way to avoid that and count only text paragraphs from the content/article?

  12. Von says

    Is there a wordpress tag to have a ‘content/block’ automatically injected into the middle of my blogs.

    I’m looking for documentation (I’d seen it before). I would appreciate any help here.

    Thank you.

    • WPBeginner Support says

      For what it sounds like you’re wanting, you would want to use the recommendation from the article


  13. Willys says

    Hi, I have just Signed up to Google adsense but I am having a problem of how to copy my Google adsense code to my website (WordPress). How can i do it?

  14. Sangam says

    I am using this code snippet in my child theme function.php to make ads appear in 3rd paragraph. But I want it to repeat after each 3 or 4 paragraph. How it can be done?

    • WPBeginner Support says

      As we state in the article, you would change the number in the code snippet to change where the ad appears.


        • WPBeginner Support says

          If you wanted the ad to display multiple times then inside the if statement where it has: return prefix_insert_after_paragraph
          You would want a new line where you add a copy of the prefix_insert_after_paragraph with a different paragraph number

    • WPBeginner Support says

      You would need to use the site-specific plugin method and change the 2 to a 3 on line 12


  15. Peter says

    Only the code is showing on my post no images. The code is just lying there in the post, you won’t even notice it’s there
    What can I do to change this
    I need the banners showing in the post

    • WPBeginner Support says

      You may want to ensure you added the code with all of it’s tags correctly for your ads and didn’t skip any tags.


  16. Ngo says


    Thank you for the manual code. It seems to be working for everyone but I just can’t get it to work on my site. My site is made up of only Custom Post Types. Is this why it is not working? Please what do I need to change in the code to make them display on my custom post types?

    Even ordinary widgets do not display on my site. Is there anything I can include in my functions.php to make changes display on my custom pages?

    Please help, I have tried every code on the internet and none of them works.

    • WPBeginner Support says

      If you want to use the custom code on a custom post type and the method we have is not working, you could try changing is_single() to: is_singular( $post_types = ” )
      and place the name of your custom post type in the single quotes


  17. Minh Hieu says

    Can you help me?

    How to insert ads before an ID element?

    Ex: i want to insert ads before

    Thanks you very much!

  18. JOHN HOBBS says

    Thanks for everything you provide. It’s truly appreciated. But question. This article reads

    Last updated on May 2nd, 2014

    at the top. Is the information presented still valid and up-to-date?

    You guys have been a tremendous help to me so I feel bad asking but I need to know.

    Again, thanks for the great resources you provide!


  19. says

    Please I need a clearly defined way to add more than one ad after different paragraphs.

    Reply needed urgently please.

  20. Atul says

    i am using infinite scroll featured theme and post advert plugin for ads but ads show only on first post, not next post. how can i solve it. this plugin does not repeat ads between post.

  21. Melissa says

    This is by far the easiest plugin I’ve ever used! It works perfectly and looks great. Thank you so much! WPBeginner has been a huge help to me in starting my new blog! :)

  22. Hoàng Trí says

    So I Want to insert ads by tag (Like Gym, Fitness) only two tag will show ads, and other will not. How i can do it ?

  23. Bob says

    How can I place an ad before the first paragraph..underneath the single post image? When i set the number to”0″…nothing happens?

  24. Qasim Iqbal says

    Hey, If I added the code using Post advert, would it be on every post? right?
    If not then tell me, I want that

  25. Bright Joe says

    Thank you so much for the code for the functions.php. This just lets me avoid so many unnecessary plugins.

    Thanks again!

  26. David says

    Was sincerely helpful to start off for my presentation at Kampala WordCamp 2017.
    Really incredible help from you.

  27. Adem says

    for more as one ad, change to this:

    $ad_code = ‘Ads code goes here’;

    $ad_code2 = $ad_code;

    if ( is_single() && ! is_admin() ) {
    $content = prefix_insert_after_paragraph( $ad_code, 2, $content );
    $content = prefix_insert_after_paragraph( $ad_code2, 8, $content );
    return $content;
    return $content;


  28. Shehab says

    I want to show two ads on my post. How can I do it?
    That process you showed, here I can see only one ad on every post. but I want to show two ads in every post

  29. shuseel says

    I want to implement ads code other than Adsence code using JavaScript code.

    The above code does not work when I have replaced JavaScript code on Ads code goes here.

    Please suggest me how to implement
    on the place of text.

  30. phil says


    Congratulation for this function.php code ! Works well

    I ve a question :

    How can i do for do not display adds on specific posts or pages ?

    Many thx

  31. Aditya says

    I am using Admiral theme and I want to display ads above the thumbnail of every post. What can I do ?

  32. Shubhamk says

    I installed the plugin, I created my ads in the post but it appears at the bottom, not at the start.

  33. Sally says

    Hi, I installed the plugin on my wp site and followed the instructions – post ad and edit settings to show ads on posts. But the ads don’t show on my posts. Do you know why? thanks in advance!

  34. Gail says

    Have just started using the plug-in and it works great!! A question: I now want to insert a new ad. Do I have to trash the old one and then create a new one? Or can I reserve the old one for a future use? Seems like I do have to trash it to make room for the new one. Thanks.

  35. Percival says

    I am wondering if it’s possible to insert a widget, rather than $ad_code = ‘Ads code goes here’;

    Is this possible? I can’t seem to implement the syntax correctly, as inserting a widget seems not to respect the paragraph numbering, and randomly inserts a “1” into the text:

    $ad_code .= dynamic_sidebar(‘test’);

    • hassan says

      did you mean code for ads?………
      you have to visit your ads network be it google adsense or for your ads code then insert it between insert here……….

    • Stephen says

      You have to make a Google Adsense account and make an ad within that. Then, you take the code they give you for the newly created ad and insert it there.

  36. Jagadish Prasad B says

    I want to put my ads in the middle of the content by using an editor. Pleas e suggest me how to add the code in the editor. Thanks for the support.

    • Ryan S says

      Creating shortcode is the best fit on your end, so first, you need to create Shortcode ads and then use that shortcode in your article/post.

      Ryan S

  37. Vỹ Spirit says

    Hello, After I add the above code to the function.php file, my article does not display ads, instead, and it’s adsbygoogle text …. How do I fix it?

    • hassan says

      i did mine some minutes ago and it work well try to remove the <?php if your functon.php already have it…….

  38. Enrique Grobbelaar says

    This app seems to work for everyone, except me. :-( I have followed all the steps, said it should display after paragraph 3. Now there is a large white block on all posts below paragraph 3, but no ad.

    Does somebody perhaps know what I am doing wrong?

    (Using Google Adsense)

  39. Great says

    Thanks you for the tutorials, I used the post ad on my site is working perfectly well but can not open in a new window. Pls how do I set the post ad banners to open in a new window?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.