You’ve implemented Google Adsense in your WordPress blog. Now, you want to avoid accidentally clicking on an ad to avoid being penalized by Google.
If you’ve manually added Google Adsense to your WordPress theme, this article is for you. If you are using one of many Google Adsense Widgets, this article might be for you assuming you know how to modify an existing widget.
This article will give you an idea on how to add a simple PHP code in your template to suppress Google Ads if a user or an admin is logged in to WordPress.
We will use the “is_user_logged_in()” function to determine if a user or an admin is logged in to WordPress. A simple “if” statement is employed to display or not display the ad.
Sample 1 – The format:
<?php if (is_user_logged_in()){ } else { ?> ... Place your Google Adsense code here .... ... <?php } ?> |
Sample 2 – With actual Google Adsense Code:
<?php if (is_user_logged_in()){ } else { ?> <script type="text/javascript"><!-- google_ad_client = "pub-3054947306505000"; /* 468x60, created 10/8/08 */ google_ad_slot = "5105451758"; google_ad_width = 468; google_ad_height = 60; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <?php } ?> |
Place this code to every Google Adsense code in your template. To view the ads, simply logout of WordPress.