How to prevent displaying Adsense Ads while you are logged into Wordpress.
Displaying Google Adsense ads while you are logged in to your Wordpress blog can be problematic for 2 reasons:
1. Your page impressions stats are skewed.
Each time when you preview your own post the Google Ads are displayed. As a result your page impressions stats are skewed. You do not know how many of those page impressions came from actual visitors and how many are from post previews.
2. Displaying ads on post previews can lead to your Adsense account being terminated by Google.
When you preview a post (that is currently in draft mode), Wordpress creates a temporary URL for the post. When the Adsense ad is displayed in the preview post the Google Adsense bot will try to access this URL. Since the page is no longer available, your web server will return a 404 Page Not Found error message. Displaying Adsense ads on 404 pages is strictly prohibited by Google. Therefore, if you preview your post multiple times before publishing it your Adsense account may be banned by Google.
Because of the above mentioned reasons, it is a good practice to tun off Google Ads when you are logged into your Wordpress blog or previewing posts.
If you are using an Adsense plugin it might contain a setting to turn off ads when a user is logged in or previewing a post. But, if you did not use any Adsense plugin and manually placed Adsense code you can prevent the ads from appearing using the following php code:
Note: You have to find the php file that calls the Adsense code and add the following checks.
Example PHP Code Snippets:
Code to block ads when the user is logged-in.
<?php
if (” == $user_ID) {
// user id is null, user not logged in
// call to display Adsense Ad
}
?>
Code to prevent ads from displaying during post previews.
<?php if (!is_preview()): ?>
// Code to display Adsense Ad //
<?php endif; ?>
