Google AdSense is an undisputed ruler of the online advertisement market. As being an AdSense publisher, you always have a greater potential to make huge money online as compared to any other advertising program.
Google always comes up with innovative ways to monetize your sites so as to increase the mutual benefit. I’ve already covered some of the highest paying AdSense niches to maximize CPC & CTR previously. Make sure to check it out.
Today I will be talking about AdSense Auto Ads, which is the most advanced form of online advertising ever.
Unlike traditional ways of monetization that requires you to add individual ad codes to various parts of your site where you want to display ads, Auto Ads use machine learning to make smart placement and monetization decisions on your behalf. It is convenient, fast, safe, and increases your income.
Just place one piece of code in the header of your pages and AdSense will take care of the rest. One problem with this approach, however, is that in CMS like WordPress, when you add code to site’s header, it injects code to the entire website.
What if you want to show Auto Ads only on specific pages? I have a solution for that. Let’s get right into it.
Display Google Auto Ads Conditionally In WordPress
We will use conditional functions in WordPress to get the job done. The code mentioned below with proper modifications will help you to show Auto Ads anywhere you want in your WordPress site.
Add the code given above in the funtions.php file of your WordPress theme. You can find it in:
Appearance >> Theme Editor >> Theme Functions (functions.php)
After adding the code, modify it according to the instructions given below.
Understanding Code
- In line 4, if (is_single()) is condition to execute the code
- In line 9, pub-xxxxxxxxxxxxxxx is your AdSense publisher ID which can be found in the Account information section of your AdSense account. Replace it with yours.
We will be making changes to the line 4 to insert your AdSense Auto Ads conditionally. Change the line 9 only once to add your AdSense publisher ID.
NOTE: All the conditional tags mentioned below have to be used in the place of if (is_single()) according to your conditional requirement.
Display Auto Ads only on WordPress Home
if (is_home())
- This will insert your Auto Ad code to the header of the main blog page. If the “Posts page” has been set to a Static Page in the Settings >> Reading, then only it will work.
Display Auto Ads only on WordPress Front Page
if (is_front_page())
- This will add the ad code to the header when the front page of the site is displayed, regardless of whether it is set to show posts or a static page.
Display Auto Ads on all WordPress Single Posts
- Don’t replace anything in line 4. The conditional tag is already set to show auto ads on all single posts.
Display Auto Ads only on specific WordPress Single Post(s)
if (is_single( ’18’ ))
- Where 18 is the Post ID of that specific post and the ad code will be inserted to the header of the post with ID 18.
if (is_single( ‘Hello World’ ))
- “Hello World” is the Post Title, and the ads will be shown only when it is the title.
if (is_single( ‘honey-comb’ ))
- “honey-comb” is the Post Slug and the ads will be shown for this particular post only having the slug.
if (is_single( array( 18, ‘honey-comb’, ‘Hello World’ )))
- It will insert the ad code to the header of single posts for the Post ID 18, Post Slug “honey-comb”, and Post Title “Hello World”.
if (is_single( array( 17, 19, 1, 11 )))
- It will insert the ad code to the header of single posts for the Post IDs 17, 19, 1, and 11.
if (is_single( array( ‘honey-comb’, ‘pea-soup’, ‘chilli’ )))
- It will insert the ad code to the header of single posts for the Post Slugs “honey-comb”, “pea-soup”, and “chilli”.
if (is_single( array( ‘Hello World’, ‘Pea Soup’, ‘Chilli’ )))
- It will insert the ad code to the header of single posts for the Post Titles “Hello World”, “Pea Soup”, and “Chilli”.
Display Auto Ads on all WordPress Single Posts, Pages, and Attachments
if (is_singular())
- It displays the auto ads on all the WordPress single posts, pages, and attachments with no exceptions.
Display Auto Ads on all WordPress Pages
if (is_page())
- It will display auto ads only and on all the WordPress pages.
Display Auto Ads only on specific WordPress Page(s)
You can use different parameters inside the if (is_page()) just like we did for the posts. For more information, have a look at the WordPress developers guide.
Display Auto Ads on all WordPress Categories
if (is_category())
- It will display auto ads only and on all the WordPress categories.
Display Auto Ads only on specific WordPress Categories
You can use different parameters inside the if (is_category()) just like we did for the posts. For more information, have a look at the WordPress developers guide.
Display Auto Ads on all WordPress Tags
if (is_tag())
- It will display auto ads only and on all the WordPress tags.
Display Auto Ads only on specific WordPress Tag(s)
You can use different parameters inside the if (is_tag()) just like we did for the posts. For more information, have a look at the WordPress developers guide.
Display Auto Ads on all WordPress Taxonomies
if (is_tax())
- It will display auto ads only and on all the WordPress taxonomies.
Display Auto Ads only on specific WordPress Taxonomies
You can use different parameters inside the if (is_tax()) just like we did for the posts. For more information, have a look at the WordPress developers guide.
Display Auto Ads on all WordPress Archives
if (is_archive())
- It will display auto ads only and on all the WordPress archive pages. Category, Tag, Author, and Date based pages are all types of Archives in WordPress.
Display Auto Ads on WordPress Search Pages
if (is_search())
- It will display auto ads only and on all the WordPress search pages.
Display Auto Ads on WordPress 404 Not Found Pages
if (is_404())
- It will display auto ads only and on all the pages when an “HTTP 404: Not Found” error occurs.
Multiple Conditions
Display Auto Ads if 2 Conditionals are met
if ( is_single() || is_page() ) )
- Inserts ad code to the header if it’s either a single post or a single page.
if ( is_archive() && ! is_category( ‘cheese’ ) )
- Inserts auto ads if it’s an archive page for any category EXCEPT cheese.
Display Auto Ads if 3 Conditionals are met
if ( $query->is_main_query() && is_post_type_archive( ‘products’ ) && ! is_admin() )
Inserts AdSense auto ads code to the header if:
- It is the main query on a custom post type archive for Products
- And if we’re not in the WordPress admin
if ( is_post_type_archive( ‘movies’ ) || is_tax( ‘genre’ ) || is_tax( ‘actor’ ) )
Displays auto ads in the conditions if:
- It is a custom post type archive for Movies
- Or it’s a taxonomy archive for Genre
- Or it’s a taxonomy archive for Actor, do something special
You can create as many conditional combinations as you want, and your AdSense auto ads will be displayed accordingly. For more information, you can have a look at official WordPress conditional tags codex.
I hope this tutorial was able to help you and setup AdSense auto ads on specific pages of your WordPress site. If you have any doubts or queries, then feel free to drop a comment in the comments section below.