How to prevent Google Adsense from loading when users are logged in to WordPress

When running a WordPress site there are times when you don’t need to load certain parts of your theme when you’re logged in. One example of this is Google Adsense, loading your own ads is not a good practice and to prevent accidental impressions you can prevent loading Adsense while users are logged in to the site with a simple piece of code.

Luckily WordPress makes it something pretty simple to do, the only thing you need to do is to copy and paste the code snippet below and replace ***Your Code Here*** with the Google Adsense code or any other element (e.g., HTML, PHP function, etc.) of your website you don’t want to see during a user session.

<?php if(!is_user_logged_in()) { ?>
***Your Code Here***
<?php } ?>

Basically the above snippet is an IF statement that checks when a user is logged-in, if there is one, then is going hide or prevent loading whatever code is inside of it.

Leave a Reply