is_admin_page() for WordPress

One of the features of my Popularity Contest plugin was breaking the post preview feature in the admin section of WordPress. I had forgotten that post content would be displayed in the admin area using the same functions I was hooking into. Of course, I already had checks in place so that the popularity wasn’t increased in these cases, but the plugin did make the assumption that the views were in the external blog interface so they broke when certain expected data wasn’t available.

I was expecting there to be an is_admin_page() function I could use to check if the current page was in the admin section or not, but I didn’t find one so I’ve added this to the plugin. It is wrapped in a function_exists check, so when this function is hopefully added to the WP core in the future, it won’t cause a problem.

Initially, I just did a check in the URL for wp-admin, but that’s really not a good solution. I had an IM chat with Dougal and he first suggested checking for get_bloginfo('wp_uri').'/wp-admin/', but we decided that sub-domains could be problematic there. Then he suggested checking of the existence of a function from admin-functions.php. This seems like the best idea to date and it will be in the next release of the plugin.

Here is the function I’m using:

if (!function_exists('is_admin_page')) {
  function is_admin_page() {
    if (function_exists('check_admin_referer')) {
      return true;
    }
    else {
      return false;
    }
  }
}

Feel free to share other/better ideas in the comments.