⏲️ Estimated reading time: 3 min
🛡️ Disable AdSense for Logged-In Users in WordPress (Plugin with Code)
If you’re running Google AdSense on your WordPress site, it’s important to exclude logged-in users especially admins from seeing or accidentally clicking ads. This post provides a free plugin and full code to automatically disable AdSense for logged-in users.
When running ads on your WordPress site using Google AdSense, you might want to exclude ads for logged-in users especially if you’re managing content, editing posts, or working on layout changes. Google frowns upon unnatural ad impressions and clicks, and it’s good practice to hide ads from contributors or admins.
Instead of manually editing every ad placement, this tutorial gives you a simple way to automatically remove AdSense for logged-in users using a custom plugin.
WordPress post including the plugin code and a download button for the ZIP file that disables AdSense for logged-in users.
⚙️ Why Disable AdSense for Logged-In Users?
Here are the most common reasons:
- Avoid accidental ad clicks by admins
- Prevent unnecessary ad impressions
- Keep your dashboard clean and fast
- Comply with Google AdSense policies
🔌 Plugin Features
- Detects if the user is logged in
- Removes AdSense
<script>
and<ins>
tags from HTML output - Works sitewide no matter where you’ve placed your ads
- No settings required, just activate and go!
🧩 Full Plugin Code
Here’s the full code of the plugin so you can copy and paste if preferred:
<?php
/**
* Plugin Name: Disable AdSense for Logged-In Users
* Description: Automatically removes AdSense code from pages when users are logged in.
* Version: 1.0
* Author: HelpZone
*/
add_action('template_redirect', 'hz_disable_adsense_for_logged_in_users');
function hz_disable_adsense_for_logged_in_users() {
if (is_user_logged_in()) {
ob_start('hz_filter_adsense_output');
}
}
function hz_filter_adsense_output($content) {
// Regex to detect AdSense scripts and ad elements
$patterns = [
'/<script[^>]*adsbygoogle\.js[^>]*><\/script>/is',
'/<script[^>]*>[^<]*adsbygoogle[^<]*<\/script>/is',
'/<ins[^>]*class="[^"]*adsbygoogle[^"]*"[^>]*><\/ins>/is',
];
foreach ($patterns as $pattern) {
$content = preg_replace($pattern, '', $content);
}
return $content;
}
📥 Download the Plugin
To make your life easier, we’ve packaged the plugin into a ready-to-use .zip
file. Just download and install it like any other plugin.
👉 Download Disable AdSense for Logged-In Users Plugin
Installation Steps:
- Go to Dashboard → Plugins → Add New
- Click Upload Plugin
- Select the downloaded
.zip
file - Click Install Now and Activate
That’s it! No settings or configurations needed.
🔔For more tutorials like this, consider subscribing to our blog.
📩 Do you have questions or suggestions? Leave a comment or contact us!
🏷️ Tags: adsense plugin, hide adsense for admin, disable adsense login, wordpress adsense, adsense settings, logged in users, remove ads wordpress, adsense optimization, dashboard cleanup, wordpress plugin
📢 Hashtags: #AdSense, #WordPressTips, #WPPlugins, #WordPressAds, #DisableAds, #WebMonetization, #WPDev, #LoggedInUsers, #PluginDev, #BlogMonetization
🔚 Wrap-Up
Disabling AdSense for logged-in users is a smart move for both policy compliance and user experience. Whether you’re an admin, editor, or just testing changes, this lightweight plugin ensures your monetization strategies don’t interfere with your workflow.
Leave a comment and let me know if you want the same functionality for Media.net, Ezoic, or any other ad platform as well.
Only logged-in users can submit reports.
Discover more from HelpZone
Subscribe to get the latest posts sent to your email.