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: WordPress.org, Code Canyon, WPmuDev.org, 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.
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.
- Install the Ad Injection plugin
- Go to Settings–>Ad Injection
- Scroll down to the Adverts and choose the ad area to update
- Drag the corner of the input box larger so you have a larger input area
- Input your conditional PHP code
- To make updating this easier, I commented each conditional area with a START and END comment to make replacing the HTML easier
- Input your HTML Ad code that pulls in the image and the URL it is linked to
- Click any of the “Save all settings” buttons to save your work
- Go to any blog post in the appropriate category to see your conditionally displayed banners.