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; ?>
Aaron,
I have tried all the plugins for wordpress I know of that do this. None of them seem to work.
I am attempting to do the code as you described. Having issues with the “// user id is null, user not logged in” part.
can you give me a list of variables here and what this would do? So if the User ID was Null would that show the ad?
Thanks a lot buddy, it was a problem for me till I found your post.
Now I can freely preview my posts before publishing….
I’m happy now..
That idea was great and helped me a lot.
But there is a problem these days, i’m not getting ads displayed on my website at all??
Can you help me with this?
Thanks anyway.