Detecting Adblock on your WordPress site

Detecting Adblock on your WordPress site

Detecting Adblock on your WordPress site

Loved by some, hated by others, Ad blockers have become an important factor to consider when owning or developing a website. In this article, I’m demonstrating a few techniques which can be used in order to detect Ad blockers on a website or most specifically, a WordPress blog.


Should you do something against people visiting your site with an ad blocker on? That’s the question many site owners are asking themselves since a few years.

Since the web exists, advertising has been a way for webmasters and site owners to make money. As ads brought lots of money, many people abused the model, displaying video ads, ads blocking contents, popups, etc. Even today, visiting a download site or just watching YouTube videos without an ad blocker is a complete pain. Approximately ten years ago, some people bothered by advertising abuse started to fight back. Ad blockers were born.

Some websites decide to do nothing against ad blockers. People who are using ad blockers aren’t likely to click on ads anyways, so it kinda makes sense. Other websites choose a diplomatic approach, asking their readers to whitelist their site, or make a small donation. And finally, some site owners choose to fight back against what they consider a threat to their revenue, by disabling features to readers who are using an ad blocker.

This article aims to demonstrate a few techniques which can be used to detect ad blockers or hide content from visitors with an ad blocker on. The way you use it on your website is up to you.

Basic JavaScript Adblock detection

The first thing to do is to create a file named adframe.js. This file is using the blacklisted name “adframe” so we’re gonna fool Adblock and make it block the file, mistaking it for a genuine ad. In the file, simply put the following line:

adblock = false;

Now, put the following withing the <head> and </head> tags of your HTML document.
On a WordPress Theme, the head section is displayed in header.php.

<script type="text/javascript">
    var adblock = true;
<script type="text/javascript" src="adframe.js"></script>
<script type="text/javascript">
    if(adblock) {
          //adblock is installed

Hiding content from users with ad blockers on

Hiding content from users with an adblocker is definitely easy. The only thing you have to do is to pick up one class blocked by Adblock, and add it to the container of your choice.

In the example below I’ve used the class .bsarocks which is added by BuySellAds around their ad banners:

<div class="bsarocks">This won't be seen by Adblock users.</div>

WordPress plugin

While the code snippets shown above can definitely be useful, if you’re using WordPress, I definitely recommend using a plugin if you’re looking to detect/block Adblock. The main reason for favoring a plugin over a code snippet is that a plugin can be updated by its author as soon as there’s a change in the way Adblock works.

There’s a few plugins out here that specialize in Adblock (and other ad blockers) detection. The most interesting in my opinion is Ad Blocking Detector. Check out the demo on the plugin website. It detects ad blockers pretty well.


Ad Blocking Detector gives you the availability to create shortcodes to display alternative content if an ad is blocked by an ad blocker. This is especially interesting if you want to encourage your readers to disable their adblocker on your website in order to support it.

The plugin should be installed through the basic plugin installation procedure, ie. “Plugins” → “Add New” in your WordPress dashboard.

Detecting visitors with JavaScript off

Some users also choose to disable JavaScript by default, to prevent many things such as popups being open, etc. Just in case you don’t know, there’s a HTML tag call noscript which allows you to display a message to users with JavaScript off:

 For full functionality of this site please enable JavaScript.

Which approach should you adopt?

Well, it’s up to you. I personally think that things aren’t all black or all white. Sure thing, ads are an important source of income for web publishers and many websites wouldn’t exist without them. On the other hand, some ads are so annoying that people who use an ad blocker can definitely be understood.

The only sure thing is that, as a website owner, you shouldn’t display “aggressive” ads to your visitors. Pop unders, video or flash ads, etc, in my opinion those should be totally banished from a serious and professional website. On the other hand, I have no problem with related banners ads, as such as the ones you can purchase on

WordPress: Super useful WP-config tips

WordPress: Super useful WP-config tips

WordPress: Super useful WP-config tips

The wp-config.php file is the heart of your WordPress website, where you can define a wide variety of options to control how your WordPress install works. Today, I’m sharing with you a list of super easy tips to take more control of your site using wp-config.php.

Keeping your database and site size small

On large websites, keeping your database small in size can be a challenge. WordPress tends to store a lot of data in your db, like transients or post revisions.

You can easily limit post revisions to a number of your choice (3 in this example) by adding the following in your wp-config.php file:

define('WP_POST_REVISIONS', 3);

If you don’t feel like using the post revision feature at all, you can simply disable it:

define( 'WP_POST_REVISIONS', false );

Also, WordPress stores in the database posts, pages, attachments and comments which have been moved to trash. You can control the number of days it will stay in the trash before being completely deleted. The default is set to 30, but I’ve set it to 1 in this example.

define( 'EMPTY_TRASH_DAYS', 1 );

By default, WordPress creates a new set of images every time you edit an image and when you restore the original, it leaves all the edits on the server. Defining IMAGE_EDIT_OVERWRITE as true changes this behavior.

define( 'IMAGE_EDIT_OVERWRITE', true );


If your web hosting plan supports SSL, you should definitely use that feature to add an extra layer of security to your site. Via wp-config.php, WordPress makes it easy to force SSL logins:

define('FORCE_SSL_LOGIN', true);

And same goes with the admin area of your site:

define('FORCE_SSL_ADMIN', true);

Secret keys are making your site harder to hack by adding random elements to the password. There are currently 4 secret keys and 4 salts that can be defined. To generate unique and secure secret keys, just use this handy generator. Don’t use those below!

define( 'AUTH_KEY',         't`DK%X:oxy|e-Z(BXb/f(Ur`8#~UzUQG-^_Cs_GHs5U-&Wb?pgn^p8(2@}IcnCa|' );
define( 'SECURE_AUTH_KEY',  'D&ovlU#|CvJ##uNq}bel+^MFtT&.b9{UvR]g%ixsXhGlRJ7q!h}XWdEC[BOKXssj' );
define( 'LOGGED_IN_KEY',    'MGKi8Br(&{H*~&0s;{k0tS(O:+f#WM+q|npJ-+P;RDKT:~jrmgj#/-,[hOBk!ry^' );
define( 'NONCE_KEY',        'FIsAsXJKL5ZlQo)iD-pt??eUbdc{_Cn)4!d~yqz))&B D?AwK%)+)F2aNwI|siOe' );
define( 'AUTH_SALT',        '7T-!^i!0,w)L#JK@pc2{8XE[DenYI^BVf{L:jvF,hf}zBf883td6D;Vcy8,S)-&G' );
define( 'SECURE_AUTH_SALT', 'I6`V|mDZq21-J|ihb u^q0F }F_NUcy`l,=obGtq*p#Ybe4a31R,r=|n#=]@]c #' );
define( 'LOGGED_IN_SALT',   'wu$4c$Hmd%/*]`Oom_(hdXW|0M=X={we6;Mpvtg+V.ol$|#_}qG(GaVDEsn,~*4i' );
define( 'NONCE_SALT',       'a|#h{c5|P &xWs4IZ20c2&%4!c(/uG}W:mAvyjI44`jAbup]t=]V7`}.py(wTP%%' );

For client sites

When using WordPress to build a site for a client, a developer is almost all the time concerned that the client won’t do something stupid to the site, requiring a lot of maintenance.

One common thing is a client trying to edit one of the site php files and ending up deleting something important, causing the website to be unavailable. You can actually prevent this to happen by disabling the built-in files editor in wp-config.php:

define('DISALLOW_FILE_EDIT', true);

Another “classic” client mistake is to never update the WordPress core, which leads to potential security breaches. You can force WordPress to update itself automatically by using the WP_AUTO_UPDATE_COREconstant in your wp-config.php file.

define('WP_AUTO_UPDATE_CORE', true);


If your WordPress install prompts you to fill in your FTP credentials each time you need to update a plugin, you can actually save a lot of time by using wp-config.php to memorize it. The three constants below will tell WordPress what are your FTP host, username and password. That way, you won’t have to submit the info each time.

define('FTP_HOST', '');
define('FTP_USER', 'Your_FTP_Username');
define('FTP_PASS', 'Your_FTP_password');

Most quality hosting companies provide SSL to their clients. If your host does, make sure you turn SSL FTP connections on for some extra security.

define('FTP_SSL', true);


When your website is unavailable or behaves strangely, you can make your maintenance work way easier by using wp-config.php and the debugging constants.

Enabling WP_DEBUG will cause all PHP errors, notices and warnings to be displayed.

define('WP_DEBUG', true);

As errors will be displayed on the site and accessible to visitors, a way more elegant way to debug is to use a log. Doing so in WordPress is easy: Once you have set WP_DEBUG to true, you can use WP_DEBUG_LOG, a constant that will make WordPress send all PHP errors and warning into a log located in your wp-content directory.

define( 'WP_DEBUG_LOG', true );

If your database is broken, you can actually repair it easily by accessing the script located at /wp-admin/maint/repair.php after setting WP_ALLOW_REPAIR to true:

define( 'WP_ALLOW_REPAIR', true );

Please note that those constants are intended to be used only when debugging a site. Once you found and fixed the problem, remember to set the values to false!


The wp-config.php file allows some tweaking to ensure a better performance by WordPress. One thing to start with is to increase the maximum memory allocated. Please note that this won’t work if your hosting provider limits the memory, which is often the case on shared hosting.
If you’re looking for a quality web host, I recommend VidahostWP Engine or InMotion Hosting.

define('WP_MEMORY_LIMIT', '96M');

You can also allow even more memory to administrative tasks (which requires more memory than just browsing the blog):

define( 'WP_MAX_MEMORY_LIMIT', '256M' );

WordPress has a built-in caching system, located in wp-content/advanced-cache.php. It can be activated using the WP_CACHE constant:

define( 'WP_CACHE', true );

Source By

How to Add custom attribute to image using the_post_thumbnail

How to Add custom attribute to image using the_post_thumbnail

How to Add custom attribute to image using the_post_thumbnail

Sometimes you will need to add a custom attribute to an image. This is very easy to do using the_post_thumbnail() function. The following snippet will add the custom attribute of 'itemprop="image" to your images but you can change that to nearly anything including class, alt, title etc.



the_post_thumbnail( 'thumbnail', array('itemprop'=>'image') );

( WordPress codex functions, hooks, in this snippet. )


How to Track post views without a plugin using post meta

How to Track post views without a plugin using post meta

How to Track post views without a plugin using post meta

Use the following snippet to track post views on your wordpress blog. The first thing you want to do is add this snippet to the functions.php of your wordpress theme. Follow step 1. and step 2. to track and display the number of views for each post. Updated this snippet from my original post on March 3rd, 2011 to Included a option for Fragment Caching so this snippet will work even on cached pages.



function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    return $count.' Views';
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        update_post_meta($postID, $count_key, $count);
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Step 1.

This part of the tracking views snippet will set the post views. Just place this snippet below within the single.php inside the wordpress loop.




Fragment Caching

Note: If you are using a caching plugin like W3 Total Cache, the method above to set views will not work as the setPostViews()function would never run. However W3 Total Cache has a feature called fragment caching. Instead of the above use the following so the setPostViews() will run just fine. Tracking all your post views even when you have caching enabled.



<!-- mfunc setPostViews(get_the_ID()); --><!-- /mfunc -->

Step 2.

The snippet below is optional, so use this if you would like to display the number of views within your posts. Place this snippet within the loop.



          echo getPostViews(get_the_ID());

( WordPress codex functions, hooks, in this snippet. )

delete_post, wp_head, get_post, get_the_ID, the_ID, add_post_meta, delete_post_meta, get_post_meta, update_post_meta, remove_action, wp,