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=trueIt 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.
This site runs on the Thesis WordPress Theme
If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.
If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:










{ 2 comments… read them below or add one }
Recently I was searching over the net about this and I found that WP have a function that tells you what is the status of the post, so it might be more trustful. The use is:
$post->post_status;
it may return:
publish|pending|draft|private|static|object|attachment|inherit|future
you may possible need to register “global $post” before using, but I didnt need to do so to use it on single.php
btw, I wish I was that good using Regex as you. :S
Very Nice Post!
Twitter: terrianns
November 23, 2009 at 2:31 pm
Great tip Farm News. I’ll have to incorporate that into the post one of these days. And regex is not actually my strong suit, you’d be surprised how many times I have to test a regex before it works. That’s why I keep this blog – to store all my favorite useful tricks and code examples at my finger tips :)
{ 1 trackback }