How To Disable AdSense Ads To Logged In WordPress Users

If you are running AdSense Ads on a WordPress blog then you might wanna disable Advertisements for logged in WordPress users.

If ads are enabled to logged in WordPress users then the author might indirectly inflate ad impressions or he might click on a Ad by mistake.

This can be a serious issue since inflating ads or clicks is strictly against Google AdSense Policies. So its better to disable ads to logged in WordPress users.

Also if you target the advertisements only towards visitors instead of authors then it can dramatically increase the performance of ads on your website.

So in this post i will share how to disable AdSense ads to logged in WordPress users as well as how to disable ads on Post Preview Page.

#1) Disable Ads Only For Logged In Users

Include the following Code to display Adsense Ads. Just put the advertisement code inside the PHP conditions. So the AdSense advertisement will be only displayed to logged in WordPress users.

This will work in any .php file like Single Post file. But if you are placing an advertisement in Widget then this fails because the code is PHP.

If you want this to work in Widgets then you can use a plugin called ‘Executable PHP Widget’.

If you don’t want to use any plugin then check my post on how to execute PHP code in WordPress widget.

 #2) Disable Ads In Preview Mode

To disable advertisements in preview mode, you have to again use the same concept of PHP condition but slightly different code. This code simply checks if preview mode is on or not. If not then displays the advertisement.

And again since this is a PHP code, it won’t work in Widget unless you use a plugin or modify the functions of your theme.

How To Execute PHP Code in WordPress Text Widget

Have you tried to execute PHP code in WordPress widget? If you did then you must be aware of the fact that PHP code doesn’t work in WordPress text widget.

There is no way you can directly add PHP code in any WordPress widget. But if you want to do so, you can make use of a Plugin called ‘Executable PHP Widget’ or just follow the steps given below.

In this post, i’ll show you how to execute PHP code in WordPress text widget without using any plugin. For this you need to edit the functions.php file of your WordPress theme.

You can find this file by browsing through –

Admin Dashboard > Appearance > Editor > Functions.php

Just copy paste the code given below at the end of the file and save the file.

Now try and add PHP code to the default text widget in WordPress. Now it should definitely work!

Additional Info

add_filter is nothing but just a WordPress hook which helps to modify variety of text before sending it out to the browser. We are adding a hook to the filter called widget_text with help of function codefap_execute_php and priority is 99. And in this case 99 will return a low priority because numbers with lower value have higher priority.

In the function codefap_execute.php, we are trying to determine whether the html code contains <?php string. If it does then we call ob_start(). The ob_start function basically turns on output buffering. This means the text produced by text widget is temporarily saved in some internal buffer.

Then we evaluate html produced as a PHP script. Finally get the contents from output buffer, clean the output buffer, turn it off and return the produced output.

For more amazing posts, Subscribe to CodeFap Blog Feed.

How To Add Placeholder Text In Subscribe Box Input

I have previously posted on how to create a simple Feedburner subscribe box for Blogger and WordPress.

The Simple Feedburner Subscribe Box contains no fancy designs but just plain text and a input form with a submit button. But did you notice that the input box is empty?

What we are going to do is just add a placeholder text (dummy text) inside the input box. And how to do that is exactly the subject of this post.

The simple subscribe box code is given below –

Note: You have to replace USERNAME with your feedburner USERNAME

The code above generates a subscribe box as shown in the image below –

Simple Subscribe Box Without Placeholder

And you can see it contains no placeholder text in the input area. To add placeholder text, modify the 7th line in the code given above. The changes are as follows:

Previous Code

New Code

After making the changes, you will find a placeholder text “Enter Your Email Here” in the input area of the Subscribe Box.

Simple Subscribe Box With Placeholder

I hope this helps and for more amazing posts, subscribe to CodeFap blog feed.