How to Insert Ads into Posts by Category

We recently received a request for a client wanted to serve up different banner ads on his website based on the category the post was in.  They wanted ads on blog posts to show relevant ads for products in their eCommerce store.  And my client wanted to be able to update the banner ads as needed.

Seems like a pretty straight forward request.  We both assumed there would be a plugin available for such a need.  All we needed to do was find the plugin, purchase it (if needed), configure it, and drop in the ad code and we would be done.  We checked everywhere:, Code Canyon,, Google, and every plugin site I know of. I checked on developer forums and reached out via social media.

Imagine our surprise when such a plugin did not exist.

Sure, there are plenty of solutions available for inserting ads into a WordPress site, but they all seem to be designed with Google Adsense in mind. With Adsense ,and most other banner services, you don’t have control over what banner ads are actually served up.  You drop in the ad code as provided, and the ad network dynamically serves up banners depending on their own algorithms.

That doesn’t help us. We need to serve specific ads on specific posts in specific categories.


Hmmm…. there must a better solution than hard coding it into a page template.

Now we could have coded things into a page template, but then we are dealing with the source code of the site.  Any time you want to swap out a ad, you have to go into the page template to make the change.  That doesn’t help my client.  By his own admission he has only rudimentary HTML skills.    The last thing I want is an inexperienced person mucking around in the php code.  That is a recipe for problems. Sure… we could develop our own plugin to do what we needed, but that takes more time and outside of the budget of my client. 

So I walked away from the problem for a few hours to clear my head and approach it from a different angle.

The answer came to me out of the blue while getting my 4 year old ready for bed.

…if we can just dynamically insert PHP into a post like Adsense code can be inserted

After a bit more research we finally found  a Adsense plugin for WordPress (AD Injection) that would also insert PHP code, not just script!  It’s designed to insert ads, but the added ability to insert PHP code is what made this the perfect solution.  With a bit of conditional php code, we now have a solution that inserts specific ads based on post category, keeps the client out of the source code, can be quickly updated with some basic HTML, and is theme independant so even if the theme is swapped out it will continue to work.

How to use the Ad Injection Plugin to Insert banners conditionally
  1. Install the Ad Injection plugin
  2. Go to Settings–>Ad Injection
  3. Scroll down to the Adverts and choose the ad area to update
  4. Drag the corner of the input box larger so you have a larger input area
  5. Input your conditional PHP code
  6. To make updating this easier, I commented each conditional area with a START and END comment to make replacing the HTML easier
  7. Input your HTML Ad code that pulls in the image and the URL it is linked to
  8. Click any of the “Save all settings” buttons to save your work
  9. Go to any blog post in the appropriate category to see your conditionally displayed banners.

Ad Injection and Conditional PHP to insert Ads into Posts by Category.


Vincent Wondra

Founder & President of BBS, Bootstrap Business Solutions a WordPress website development and design agency. He believes that every business deserves an easy to update, scalable, versatile, and stable website. Follow me on Google