Turn Off Analytics In Preview Mode With Wordpress

3rd June 2008 - 2 minutes read time

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

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

Goackefsces (Tue, 10/21/2008 - 04:18)

Add new comment

The content of this field is kept private and will not be shown publicly.