Hide adsense ads for WordPress admin with GTM

Most of WP admins who don’t like to serve ad impressions while editing / previewing blog posts nor want to use browser plugins like AdBlock (that creates file upload and lot other issues)

In this tutorial, we will discuss of blocking Adsense ads whenever a user is logged in with Admin or Editor permissions using Google Tag Manger.

Plan: (1) detect user-type in WP (2) trigger a GTM custom event data-layer for based on user-type (3) based on event – control the adsense tag parts to block the ads

Below PHP code will conditionally detect if the user type is Editor or Admin and echos value based on the rules.

Above code will work for both “editor” and “administrator”, in case if you want only administrator to be blocked and delete off

and it will be:

After adding PHP script to  GTM event datalayer script:

So, above code will send GTM event “noAds” if user is admin and send “showAds” if user is not a  admin.

Now we want to configure GTM,  before that I hope you know that for AdSense to trigger an ad, it needs a line of JS code to be executed at least once in the content page .

And  this is the script that we will be controlling in GTM. So, keep in mind that above code should not be anywhere in content other than at GTM.

Now let us create custom HTML tag in GTM for

and create a new firing trigger (selecting more)  based on  custom event “showAds”.

We are done! now publish the tags and have a preview in GTM.