How to Suppress Ads when WordPress Users are Logged in?


Many bloggers put ads on their websites. But they spend (like me), lots of time browsing their own articles in order to spot errors or find rooms for improvements. The ads impressions unfortunately will still be counted so the CTR (Click Through Rate) may not be so accurate.

You can suppress all ads using ad-blocker browser plugin. Alternatively, before you insert the Ads code, you can check if user logged in using wordpress API get_current_user_id() which returns the user id of the current user, if applicable. If none, then this function simply returns zero.

1
2
3
4
5
6
$ad_code = 'Ads code put here..';
if (get_current_user_id()) {
  $ad_code = '';
}
 
echo $ad_code;
$ad_code = 'Ads code put here..';
if (get_current_user_id()) {
  $ad_code = '';
}

echo $ad_code;

Or in this form:

1
2
3
4
5
6
7
8
9
<php
if (!get_current_user_id()) {
?>
 
// HTML code of the ads
 
<php
}
?>
<php
if (!get_current_user_id()) {
?>

// HTML code of the ads

<php
}
?>

After this is done, only when logged out (like other visitors), the ads will be shown. As long as any user logged in, the ads won’t be shown. Of course, if you know your own user id and want to only suppress the ads code when you are logged in (suppose it is a multi user wordpress site), you could easily change the if-check (for example, if the user id 1 is white listed).

1
2
3
4
5
6
$ad_code = 'Ads code put here..';
if (get_current_user_id() == 1) {
  $ad_code = '';
}
 
echo $ad_code;
$ad_code = 'Ads code put here..';
if (get_current_user_id() == 1) {
  $ad_code = '';
}

echo $ad_code;

Or in this form:

1
2
3
4
5
6
7
8
9
<php
if (get_current_user_id() != 1) {
?>
 
// HTML code of the ads
 
<php
}
?>
<php
if (get_current_user_id() != 1) {
?>

// HTML code of the ads

<php
}
?>

Update: The same idea can be applied to using Google Analytics tracking code, so your browsing records won’t be tracked and appear in the statistics. You could also use [Google Publisher Tool] to identify the ads (so you are less likely to click on them).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
378 words
Last Post: How to Speed Up Parallel Processing using Parallel For, Foreach in C# (.NET 4.0 or above) ?
Next Post: Greedy Algorithm to Reach the Last Index of the Array

The Permanent URL is: How to Suppress Ads when WordPress Users are Logged in?

Leave a Reply