is_admin_page() for WordPress

Posted in: Development, 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.

Popularity: 4% [?]

Posted May 25th, 2005 @ 11:41 AM

10 Replies

  1. Andy Skelton adds this Comment:

    From classes.php:
    if (strstr($_SERVER['PHP_SELF'], ‘wp-admin/’)) {
    $this->is_admin = true;
    }

    So you can do this:
    if (!$wp_query->is_admin) {
    // do stuff you can’t do in wp-admin
    }

    I think so, anyway :)

    May 25th, 2005 at 12:52 pm

  2. Andy Skelton adds this Comment:

    Oh, don’t forget to
    global $wp_query;
    if it isn’t already.

    May 25th, 2005 at 12:53 pm

  3. Andy Skelton adds this Comment:

    Or just use
    strstr($_SERVER[’PHP_SELF’], ‘wp-admin/’)
    like the core does.
    (Sorry for three comments!)

    May 25th, 2005 at 12:55 pm

  4. Alex adds this Comment:

    Umm, as discussed in the post that was my first approach and we rejected it.

    May 25th, 2005 at 1:07 pm

  5. Joao Craveiro adds this Comment:

    if (something) then true else false?!?!?! How ’bout:

    if (!function_exists(’is_admin_page’)) {
    function is_admin_page() {
    return (function_exists(’check_admin_referer’));
    }
    }

    ;)

    May 25th, 2005 at 4:34 pm

  6. Alex adds this Comment:

    I was trying to make it extra readable, but very cute… you certain showed me.

    May 25th, 2005 at 4:35 pm

  7. alexking.org: Blog adds this Trackback:

    Popularity Contest Plugin (beta 3)

    I’ve posted beta 3 of my Popularity Contest plugin. This release has 2 main changes:

    Don’t do anything on admin pages – this should make post previews work again.
    The post path to update values should work when WordPress is installed in a sub…

    May 27th, 2005 at 1:35 pm

  8. Michael Huynh adds this Comment:

    Hi Alex,

    The recent upgrade to Wordpress 2.0.2 breaks the is_admin_page() function as you defined it above. Pascal Van Hecke did a write up on his blog about this problem and suggested a solution.

    Essentially, he replaced the check for ‘check_admin_referer’ with a check for ‘write_post’.

    Hope that helps!

    April 12th, 2006 at 11:42 pm

  9. Alex adds this Comment:

    Yes, I’ve changed this to use the built-in function in new versions of my plugins.

    April 13th, 2006 at 12:07 am

  10. The Agony and Ecstasy - Creating a Wordpress Plugin | mummey.org adds this Pingback:

    [...] Wordpress has a really nice way to handle your plugin options, it provides some special hooks and takes care of all the database stuff for you which is really nice. There are a couple ways to hook into it, the route I used is to first check if the user is acutally in an admin page, I have this function, which is a modified one from here: [...]

    March 13th, 2009 at 12:12 am

Add a Comment

Please note: Use of a non-personal web site or blog in the field below and/or comments that are off-topic, personal attacks, or support requests will likely be removed at my discretion.

Note: This post is over 4 years old. You may want to check later in this blog to see if there is new information relevant to your comment.

Use Tasks in WSJ » « Online Ordering

About This Site

This is the personal web site of Alex King, a web developer in Denver, Colorado USA. More...


Crowd Favorite

Crowd Favorite is my software and web development business.

We build web applications, design and develop custom WordPress themes and plugins, and build custom sites using WordPress as a CMS.


I also have a tumblog that aggregates my online content from other services (Twitter, Flickr, del.icio.us. etc.).

America - america09.com

Ads

web design services