A Simple Guide to Controlling AdSense Display on Your WordPress Site

Control adsense display

Are you searching for a quick solution to remove AdSense code from your WordPress site’s front end when viewing preview pages or using the Customizer?

Google AdSense is a fantastic tool for monetizing your website, but sometimes you might not want ads to appear. Perhaps you’re previewing an unpublished post or tweaking your site’s appearance, and the ads are getting in the way.

In this post, we’ll guide you through a simple method to control where your ads are displayed. We’ll introduce a straightforward function that you can incorporate into your site, preventing ads from appearing for you as an editor or admin when logged in. This way, you can focus on your work and nothing else.

Control adsense display

The Hide Adsense PHP Code:

Here’s the code you need to place in your theme’s functions.php file:


// AIO Spark Adsense Code for non-logged in users
add_action('wp_footer', 'aio_adsense');
function aio_adsense(){
if( current_user_can('editor') || current_user_can('administrator') ) {
  // user is an admin, show no ads.
} else {
function correct_enqueue() {
wp_enqueue_script('adsense', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', false ); 
}
add_action( 'wp_enqueue_scripts', 'correct_enqueue' );

echo ' <script>
				!function(){const e=document.createElement("script");e.async=!0,e.setAttribute("crossorigin","anonymous"),e.src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX";let t=0;document.addEventListener("mousemove",function(){1==++t&&document.getElementsByTagName("HEAD").item(0).appendChild(e)}),window.onscroll=function(n){1==++t&&document.getElementsByTagName("HEAD").item(0).appendChild(e)},setTimeout(function(){0===t&&(t++,document.getElementsByTagName("HEAD").item(0).appendChild(e))},5e3)}();
			</script> ';
}
}

Replace the XXXXXXXXXXXXXXXX with your Adsense Publisher ID, and you’re good to go.

What Does This Code Do?

This code uses a WordPress action hook, ‘wp_footer’, to call a function named ‘aio_adsense’. This function checks if the current user is an editor or an administrator. If they are, no ads are shown. If they’re not, the AdSense code runs, and ads are shown.

Hiding Ads on Unpublished Pages

One of the key aspects of managing AdSense display is controlling the visibility of ads on unpublished pages. This is crucial as it helps you avoid violating Google’s policies. In this section, we’ll guide you on effectively hiding ads on unpublished pages and summarize some of the key points from the Google AdSense policies that relate to what we’re saying.

Publishers must adhere to certain rules to ensure a positive experience for Internet users and Google advertisers. Here are some relevant points:

  • Invalid clicks and impressions: Publishers may not click their ads or use any means to inflate impressions and clicks artificially. Clicks on Google ads must result from genuine user interest.
  • Encouraging clicks or views: Publishers may not ask others to click or view their ads or use deceptive implementation methods to obtain clicks or views. This includes compensating users for viewing ads, performing searches, or placing images next to personal ads.
  • Traffic sources: Google ads cannot be placed on pages receiving traffic from certain sources. For example, publishers may not participate in paid-to-click programs, send unwanted emails, or display ads as a result of the action of any software application.
  • Ad behaviour: Publishers can modify the AdSense ad code if those modifications do not artificially inflate ad performance or harm advertisers.
  • Ad placement: AdSense code may not be placed in inappropriate places, such as pop-ups, emails, or software. Publishers must also adhere to the policies for each product used.
  • Site behaviour: Sites showing Google ads should be easy for users to navigate. Sites may not change user preferences, redirect users to unwanted websites, initiate downloads, include malware, or contain pop-ups or pop-unders that interfere with site navigation.

Regarding unpublished pages, it’s important to remember that Google AdSense has rules about where ads can be placed.

By controlling where your ads appear, you can ensure you follow these rules and avoid potential issues. This helps maintain a positive user experience and ensures you adhere to Google’s policies.

Benefits of Conditionally Hiding AdSense Code?

  1. Better User Experience: Hiding ads can make working on your site easier. This is especially true on pages where ads might be distracting.
  2. Professional Look: If you’re working with a team, hiding ads on the backend can make your site look more professional.
  3. Save Resources: Ads use resources every time they load. By stopping them from loading on certain pages, you can save resources and make your site faster.
  4. Follow AdSense Rules: As we just mentioned, Google AdSense has rules about where ads can be placed. You can ensure you follow these rules by controlling where your ads appear.

Why We Avoid Plugins for Controlling AdSense Code

We often switch between our site’s front and back ends. We preview our work on the front end and create and edit posts on the back end. We add our AdSense code manually to the footer of our site, letting us control when it loads to not slow down our site. But we needed a way to control when the ads are shown.

We wanted to hide the ads while working on the site and show them when visitors browse. This helps us avoid distractions and follow Google’s rules whilst enabling advertisers to reach customers interested in their products and services at the appropriate time.

However, using plugins to control AdSense code often led to site slowdowns, negatively affecting our SEO and user experience. Therefore, we decided to avoid plugins and manage the AdSense code manually.

We couldn’t find a resource that showed us how to do this, so we decided to share our solution here. You can copy the code above into your child theme’s functions.php file.

Hide adsense code

Conclusion:

Finding a quick and efficient way to control where your AdSense ads appear on your WordPress site can significantly enhance your workflow. Whether previewing unpublished posts or adjusting your site’s design in the Customizer, you shouldn’t have to deal with unnecessary distractions.

With the simple function we’ve shared, you can easily prevent ads from showing up on specific pages, allowing you to focus on creating and editing your content. This improves your site’s user experience and ensures you adhere to Google’s rules. Take control of your AdSense display today and simplify your site management tasks.