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.
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.
1. AdSanity
One way to insert ads within your post content is by using the AdSanity plugin.
AdSanity is a premium WordPress ad management plugin. It allows you to place ads on any page, post, or widget-ready area using shortcode.
You can then either upload your own ads or show advertisements from third-party networks such as Google AdSense. For more information, see our guide on how to manage ads in WordPress with AdSanity plugin.
First, you’ll need to install and activate the plugin. If you need help, then please see our guide on how to install a WordPress plugin.
Upon activation, go to AdSanity » Create Ad.
To get started start, you’ll need to give your ad a title by typing into the field that shows ‘Give this ad a title.’
Your site visitors won’t see the title since it’s for your own reference only.
Next, you need to choose the kind of ad that you want to create. One option is to build your own ad and host it on your website, which is perfect for selling ads on your WordPress blog.
Another option is to use the ad code provided by a program like Google AdSense.
To start, let’s see how you can create a hosted ad using AdSanity’s built-in tools. First, click on the ‘Ad Hosted On Site’ tab.
Next, you can choose how big your advertisement will be on your website by opening the ‘Ad Size’ dropdown and choosing a size from the list.
Once you’ve done that, you’ll need to type a tracking URL into the ‘Tracking URL’ field. This is the URL that the ad will link to.
By default, this URL will open in the same browser tab. However, you may want it to open in a new tab, particularly if the advertisement is linking to someone else’s website.
To make this change, simply check the ‘Open in a new window’ box.
Once you’ve done that, scroll to the Ad Image section. Here, you can upload the image that WordPress will use as your ad.
To upload a banner or other ad graphic, simply click on ‘Set ad image.’ You can then upload or choose an existing image from your WordPress media library.
To help organize your ads, you may want to create some advertisement groups. For example, you might create a ‘holiday advertisements’ group for all the ads you plan to use around the holiday season.
To create an advertisement group, click on the Add New Group link. This adds a new section where you can type in the title for the group.
Just like WordPress categories and subcategories, you can arrange your advertisement groups into parent-child relationships.
If you have lots of different ads, then organizing them into parent and child groups can make it easier to find exactly the ad you’re looking for.
When you’re happy with how your advertisement group is set up, click on the ‘Add new Group’ button to save your changes.
To add your advertisement to a group, simply check the group’s box.
Once you’ve finished working on your ad, it’s time to publish it. If you want to show the ad right away, then go ahead and click on Publish.
Another option is to schedule the ad so it will show up on your WordPress website at some point in the future. To schedule your ad, click on the ‘Edit’ link next to ‘Publish forever.’
WordPress will now show some settings that you can use to set the date and time when this ad will be published.
You can also use these settings to choose when the ad will be put back into a draft state. When this happens, the ad will disappear from your WordPress blog or website.
If you don’t want the ad to expire, then just click on the ‘Publish Forever’ button.
You’ve now successfully created a hosted ad.
While you can sell advertising space directly to other businesses, many website owners use an advertising network instead. These networks are often hassle-free because you don’t have to deal with advertisers or worry about collecting money.
Instead, the network decides which ads to place on your site, collects the payment, takes their management fee, and then sends the rest of the money to you.
If you’re using an advertising network, then click on the External Ad Network tab.
Next, type in a title and add your advertisement to any groups following the same process described above.
You can also choose a size for your ads. If you’re using Google AdSense, 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.
Once you’ve done that, you’re ready to copy the advertisement code provided by your network. Then, simply paste this code into the ‘Ad Code’ section.
When you’re happy with how your ad is set up, you can either publish it or schedule it following the same process described above.
No matter what kind of ad you create, the next step is placing the ad on your website. The steps are exactly the same for hosted ads and ads provided by a network.
To position your ad on any page, post, or widget-ready area go to AdSanity » Manage Ads.
On this screen, find the ad that you just created and click on its shortcode link. This will copy the shortcode for this advertisement.
You can now 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 code into this block. For step by step instructions, see our beginner’s guide on how to add a shortcode in WordPress.
If you’ve already published the ad, then you can visit the page or post to see the ad within your post content. If you scheduled the advertisement instead, then the ad will appear on your site at the date and time chosen by you.
2. 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.
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.
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.
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.
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.
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.
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.
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.
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.
3. Code Method
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.
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 your code snippet, or save the changes you’ve made to your site’s functions.php file.
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.
André says
How add in last paragraph?
Shubhi says
Gr8 post and correct code. It worked smoothly…:)
Lyke Basilio says
Hi,
I just added it to my personal blog and it works perfectly!
Thanks,
Ashok Kumar says
Hi,
I added an ad just fine before my post. However, when I tried removing it, the blank white space remains. Please let me know how to fix this, my site looks terrible and i’m at my wits’ end!
Thanks,
Ashok
Ashok Kumar says
Hi,
I’ve been trying to fix this for 2 days to no avail. Could someone help?
Thanks
helper says
fix drawAdsPlace to return the content, add simple-ads-manager.php (somewhere in the class):
function get_drawAdsPlace($args = null, $codes = false) {
global $samObject;
if(is_null($args)) return ”;
if(is_object($samObject)) return $samObject->buildAd($args, $codes);
else return ”;
}
then use get_drawAdsPlace instead drawAdsPlace. (Updating plugin will kill this)
WPBeginner Staff says
This plugin allows you to add after the first paragraph, after the 9th paragraph, etc.
You can specify that in the settings of each ad.
Zaman says
I have noticed that there are around three ads in each post in list25 with one ad after every the first post while two other ads randomly between the images.
So, could you please inform how can it be achieved? Are you using the insert post ads or some manual coding for the second and third ads. If so, what is the manual code and where are you inserting it.
Annastasia Taylor says
Hi,
“Insert Post Ads” plugin setting doesn’t work for me, when i click on settings-nothing displaying.
Code works fine, Thanks!
So my question is, can i repeat this action? I mean if i can insert the ads after paragraph 1 and 2.
I tried to repeat the code, but doesn’t work : “Fatal error: Cannot redeclare prefix_insert_post_ads()…..”
Please help
Thanks in advance
Jorge Pinto says
Hi, its possible to use the above code with this one integrated?
//////////////////here mobile ad
////////////////// here desktop ad
Adjie Purbojati says
Yes, u can using wp-is-mobile
Christian Rauchenwald says
Great plugin BUT I’m missing one very important thing -> if I create 4 ads to be displayed after paragrahp two I should get the option to show one of them randomly…
It would also be great to be able to define “categories” for banner, so that a poker related banner doesn’t show up in my day trading section.
Arsie says
Hi,
For multi-author WP-blog who does not want to use plugins, can you recommend the code that I can put in the functions.php?
Resham Panth says
Hey I lke your plugin and I am using it on my blog. I would like to know how can I align my ad to right side so that the content appears on left ?
Dok Simon says
Hello !!
Is it also possible to allow Authors to show Their Ads only on their contents on the blog ??
Salud Casera says
This one really worked for me. I removed the CSS and added some inline css and got the ad before the content, float left, etc. and it ended up exactly as I wanted it.
You can see it here :
Thanks for sharing.
Jorge says
Hi guys, where can i change the css of this div:
?? Thank you
Ben Daggers says
Quick question, what if I assigned “5” as the number of paragraph for the adsense code to appear, but the number of paragraphs in my post is only four. What will happen? Will it still work?
Amanda says
disregard to text part above an ad, I figured that out
Amanda says
I have 2 qusestions:
1. Is there a way to add text above an ad? For example, amazon affiliates has a banner for $3 per sign up, but they are also doing a contest in the same signup where users can win $500, I’d like to ad a note above the referral banner for sign up about the contest, is there away to do that?
2. How can I had this to the front page?
Belinda says
Great tutorial, thanks. Is there a way for me to had the ads also show up on my homepage? I have a sticky post at the top and then my posts show up after that but this version of the posts have no ads. So ads only appear if the reader actually clicks through on a post to the individual post URL. Would be great if the ads would show also on the homepage.
steve says
What impact does this have on SEO considering there will be links to adverts within the content of the page?
Aadil says
Hello Wpbeginner,
Really a good explanation. Really like it. However I have one question, would be really grateful if you could help me. If we use the code way,Is there a way to exclude some pages like- about, contact us etc where we dont want want the ads to appear.
Antonios says
With this plugin the ads placed in a post appear in all the other posts. How can I place an ad in one specific post, and that it won’t appear on other posts?
I have posts related to specific services, products, and topics. I need to be able to place ads related to the specific post, and that won’t appear on other posts, because ads won’t be related to those other posts.
tigaruk says
How do i get it to only appear on posts pages using the code above, not the plugin. Currently its also appearing on my product pages.
dan says
Ads don t appear in posts before the plugin was instaled?
Dan Sz. says
Is there a way to get this to work within a custom loop? For example, when showing the full content of a featured post on the home page.
kristarella says
Thanks for this. I would recommend tweaking the code slightly to add the insert at the end of a post if it is shorter than the specified number of paragraphs. This can be done with:
$num = count($paragraphs);
if ($num == 0 || $num < $paragraph_id) {
return $content.$insertion;
}
verdipratama says
Really helpful, thanks
Will Nicholls says
I would like to use this put wrap the text around it. How can I do this ?
Chris says
Seems like this would be some simple css to change the appearance. Do you have an example I could see online?
Karan Singh Chauhan says
Great way to insert ads.
Brandon says
Is there a way I can do this but have it on last paragraph? I want to insert at bottom right of every post’s content and have text wrapped using div align=”right” which I can see in your code I can change the div ad container, but I want it on bottom. Thanks!
WPBeginner Staff says
Hi Josh,
We have released a new version of Insert Post Ads. Please give it a try.
Thanks
Josh M says
Very nice! Great updates Team WPB!!
Magic says
Thanks for the tutorial. What would the functions.php code be if I wanted the ad to show as the second paragraph up from the “bottom” of the page (not the second paragraph from the top of the page)?
Margaret Anne @ Natural Chow says
What do I do if there is only a blank space showing up where the ad is supposed to be? I don’t have an ad-blocking software on my computer so am I doing something wrong?
WPBeginner Staff says
Josh, thanks for the feedback. We have forwarded your suggestions to our development team.
Josh M says
Hi, great, simple plugin. Just wanted to make two suggestions.
I would recommend removing the hard-coded styling for the div or add a css box in settings, so it can be positioned easier. For those wondering, remove this in insert-post-ads.php…
style=”clear:both;float:left;width:100%;margin:0 0 20px 0;”
A simple option (checkbox?) on the post/page to “exclude content ads” would be great as well.
St3elSh0t says
Hi Syed, could you please tell me how to add 2 ads after 1st paragraph so that there is one ad then a tab then another ad code?
WPBeginner Staff says
Unfortunately, currently it is not possible.
Brandon says
You could use the code to add to functions.php and instead of using ad code here … you could do first ad code here second ad code here
Then edit style sheet to set sizes and alignment for .maincontainer
.firstad
.secondad
Andor Nagy says
Hi, Nice tutorial. Is there any way to add more then one ad? Like 1 after 2nd paragraph, 1 after 3rd and so on
ETphonehome says
I’ve installed the plugin and it shows all in the preview but the ads are not shown when I publish the post. What did I do wrong? Thanks.
Crispylogs says
did not work for me
WPBeginner Staff says
Those are hard coded in the template file single.php
Patrick says
Can we post adds at paragraph 0 , i mean on top of a post ?
WPBeginner Staff says
No you can not. The plugin only accepts values higher than 1.
Brandon says
You can edit single.php and look for a line that says something like:
<?php the_content('’); ?> or entry…
and ad your code above it and save the file
WPBeginner Staff says
The ad code is provided by your advertisement provider like Adsense.
David says
Not familiar with Adsense, could you provide an example? If I’m designing my own ad and its is a jpeg, what would the ad code be if the jpeg is called MattsAutoSales.jpeg ?? I’m used to working with print ads which usually just use the digital name for placement (MattsAutoSales.jpeg)
Thanks for the help!
SarahZigler says
Really helpful, I’m waiting for my adsense got approved. And this is a good information for me to implement adsense. Thanks.
David says
Could you define “ad code” ? I’m missing how to create one.
WPBeginner Staff says
See our guide on How to Add Facebook Like Button in WordPress
Chetan Parate says
How to add facebook like button on the WP page ??
Scott Buehler says
How are you guys inserting ads next to the featured image here on WPB?
WPBeginner Staff says
An existing plugin on your site may be conflicting. We recommend deactivating all plugins except insert post ads and then activate other plugins one by one until you figure out which plugin is causing the issue. Let us know.
Benjamin says
I just tried and deactivated all plugins other than Insert Post Ads. Same result.
You can see the problem here :
http://kieloo.staging.wpengine.com/french-expressions/
WPBeginner Staff says
Can you please switch to a default WordPress theme like Twenty Twelve or Twenty Thirteen and see if this solves the issue.
Benjamin Houy says
No. The issue remains. It may be a conflict with WP Engine’s configuration then.
WPBeginner Staff says
Check out our guide on some other ad management plugins for WordPress.
Kim Whitley-Gaynor says
Very helpful information! Thank you very much. What plug-in do you recommend for inserting ads into a sidebar?
Benjamin Houy says
Your plugin seems great. But the content of my posts doesn’t show anymore when I activate it.