I recently was thinking about how to prevent some scripts from loading, and executing, each time I logged into WordPress. The scripts were traffic statistics scripts and I didn’t want them from counting the visits from myself. If the option was available from the statistics script owner, I would simply have told it to exclude my IP address. If I decided to view my blog from a different location that I normally don’t use; however, then the script would count my visit.
The solution, as it turns out, was a rather simple one, and can be used to prevent pretty much anything from loading for a logged in user – any logged in user.
Prevent a Scripts from Running for a Logged In User
The code is actually pretty simple. As I develop my new templates I learn more about the WordPress function library, and I found a function that will help detect if the current visitor is currently logged into a WordPress blog.
The WordPress function is simply is_user_logged_in().
The function will return true if the user is logged in, or false if they are not logged in. To prevent something from loading for a logged in user, you can simply use an “if” statement to check if the user is logged in. If they aren’t (the function returns false), then you can display the script, HTML code, or whatever you wish.
For example, the following code will display “This user is not logged in” for any user that isn’t currently logged into your blog.
<p>This user is not logged in</p>
<?php } ?>
The message displayed above won’t be displayed for any user that is logged into your WordPress blog. To use this for traffic statistics scripts, you simply replace the line <p>This user is not logged in</p> with your script code. This will then prevent any logged in user from being recorded in your traffic stats.