WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to identify admin users and non-admin (but logged in) users through cookies. I need this feature because I use Hyper Cache (which uses cookies to identify those who send pages in cache): I would like to serve cached page to all users not logged in and to all users logged in but non-admin. The logged in admin users will not get cached pages.

share|improve this question
up vote 1 down vote accepted

You can use current_user_can() to detect what type of user is logged in, if any, then use setcookie() and $_COOKIE to test and set the necessary cookies.

function wpse_74742_stop_cache_cookie() {
    if (current_user_can('admin')) {
        if (empty($_COOKIE['disable_cache'])) {
            setcookie('disable_cache', 1);
add_action('init', 'wpse_74742_stop_cache_cookie');

This is an extremely basic example. There are a lot of details to cookie management so you may want to do a bit of reading up on cookie paths, domains and expiration.

You also may do better to simply set the cookie with the appropriate hook instead of relying on the user capabilities with the admin_init action:

function wpse_74742_disable_admin_cache() {
    if (empty($_COOKIE[''])) {
        setcookie('disable_cache', 1);
add_action('admin_init', 'wpse_74742_disable_admin_cache');
share|improve this answer

You can achieve this by

if ( current_user_can( 'manage_options' ) ) {
    // Code for the administrators
} else if ( is_user_logged_in() ) {
    // Code for other logged in users
} else {
    // not logged in users

Please be sure to check if only the administrators have the capability to manage_options. In the default setting, this is true, but you can alter the capabilities for each usergroup.

If manage_options is not available to the administrators only, use a different capability that only administrators have.

share|improve this answer
You don't have to check an option, current_user_can() supports checking a capability role: current_user_can('admin') but checking for a specific option can offer a more granular control over who to implement caching for. – totels Dec 3 '12 at 13:14
You should not check for roles, as they are just a freely assignable set of capabilities. You should always check against capabilities, as the role admin could be stripped from all capabilities, and author could have the possibility to do everything on the site. – fischi Dec 3 '12 at 17:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.