Turn Off Analytics In Preview Mode With Wordpress

The preview mode in Wordpress exists to allow you to see how a post will look before you publish it to your site. This is an important feature if you have lots of readers as once your click that publish button your post will be live and your RSS feed will be pinged out to lots of different services. Previewing a post can avoid embarrassing spelling or formatting errors before the post goes live.

Preview uses the same templates files as your blog and so will load all of the same files as if you normally view your site. The only small issue with this mechanism is that any analytics code you might have installed will be run as well and you might see that you most visited pages are you previewing posts before putting them live.

Of course you could just filter out your IP address in your analytics, but if your Wordpress blog has lots of different authors writing content from many different locations it is unfeasible to continuously edit your analytics settings.

A better way is to use the function built into Wordpress called is_preview() to detect if the page or post you are looking at is in preview mode. Here is an example.

if(!is_preview()){
  // analytics code here
}

An alternative method is to see if the preview parameter is set in your URL string.

if(!isset($_GET['preview'])){
  // analytics code here
}

Comments

Hey, My Name is, Christopher Nice site, very informative
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
12 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.