Block Content and Detect Wordpress Preview
I've had a problem lately where Mint has been recording my many hits to preview versions of articles and ranking it in places it doesn't belong.
I've told Mint, upon login, to ignore me but it doesn't seem to listen. So I though WordPress should step in a little.
Update
I've made an pretty big update to this concept, you can read about it here Block Tracking and Detect Wordpress Login.
I created a regular expression to detect the query string formed similar to: http://blog.ninedays.org/?p=####&preview=true
That regular expression looks like this: $pattern = '/p=([0-9]*)&preview=true/';
I grabbed the query string (everything after the ? in the URL) using the PHP $_SERVER super global: $query = $_SERVER['QUERY_STRING'];
With my pattern and string I used preg_match() to determine if the string matched the REGEX pattern and put it all together in a little function to return whether or not the entry was in a preview mode:
PHP
<?php
function is_preview_mode(){
$query = $_SERVER['QUERY_STRING'];
$pattern = '/p=([0-9]*)&preview=true/';
return preg_match($pattern, $query);
}
?>
Extra Precautions when Adding Functions to Wordpress
An extra precaution that is highly recommended when adding new functions to run in Wordpress, or any other expandable PHP tool is to ensure you are only defining the function if the function doesn't already exist. So I added that in:
<?php
if(!function_exists('is_preview_mode')){
function is_preview_mode(){
$query = $_SERVER['QUERY_STRING'];
$pattern = '/p=([0-9]*)&preview=true/';
$result = preg_match($pattern, $query);
return $result;
}
}
?>
Then I tossed it into my WordPress theme's functions.php file for testing. Eventually it should be made into a plugin for expandability but not till it's slightly more robust.
Now in my header and footer where I want to block Mint and Google Analytics I'll use this if statements:
PHP
<?php if(function_exists('is_preview_mode')){if(!is_preview_mode()){ ?>
[...]insert analytics or mint or other tracking code here[...]
<?php }} ?>
Now those elements will be blocked when I preview my article, and when I port my code to another installation or another template I don't have to worry about errors if I forget a piece since I was careful to only define an undefined function, and use a defined function. Beautifully simple code to accomplish a lot as far as accurate tracking is concerned.
Regular Expression Needs Updating
After I had implemented this solution I noticed two problems.
- The 'preview' link from an entry edit page that is published, but dated past the current date and time displays the link as the regular URL and the query string only contains
?preview=true
It goes to:example.com/2007/12/28/blog-entry/?preview=true - And that the 'view' link from the manage list in Wordpress differs from the 'preview' link in the Wordpress edit page. Yes, even on drafts.
It uses?p=###with no preview=true
So I adjusted the REGEX to the following so those pages would be blocked from Mint and Analytics also.
Important if all of your WordPress entries still use the old query string to link and pull pages (like: example.com/index.php?p=###) instead of the newer URL write_mod type of URL (like example.com/2007/12/28/blog-entry/) you need to skip the solution to step 2 or your live articles will no longer track either. You can still use step 1's updated solution though.
Step 1
For this one all we need to do is make the p=### optional. That's easy with regular expressions:
PHP
$pattern = '/(p=([0-9]*))?&preview=true/';
Step 2
Now we need to make the second half optional, but only if the p=### is there. I though about using lookahead/behinds but I'm not really good with those yet, so I went with my trusty or in two, comparing the two conditions. The first part is p=### (optional) with preview=true (required) or just p=###.
PHP
$pattern = '/(((p=([0-9]*)&)?preview=true)|(p=([0-9]*)))/';
Summary
So if you are using those smart URL's your final function will look like this:
PHP
<?php
function is_preview_mode(){
$query = $_SERVER['QUERY_STRING'];
$pattern = '/(((p=([0-9]*)&)?preview=true)|(p=([0-9]*)))/';
$result = preg_match($pattern, $query);
return $result;
}
?>
Now you won't have to worry about tracking previews, so go ahead and preview your articles till they look perfect, ain't nobody watching!
Update
I've made an pretty big update to this concept, you can read about it here Block Tracking and Detect Wordpress Login.
Popularity: 48% [?]
the newest discoveries, stories and shared tips!Come on, all the cool kids are doing it ;)


