• moroandrea

    (@moroandrea)


    I’m not a programmer, but I’m sure that I’ve used the Is_Admin function before to allow a bif of code run only when an admin is logged in.

    However, when I try to include the function in the footer.php of my customized template, the code within the call is ignored regardless an admin or a not logged in user is viewing the site without any apparent reason or error displayed on screen.

    Any help? Is is_admin() now deprecated?

    Thanks
    Andrea

Viewing 4 replies - 1 through 4 (of 4 total)
  • Big Bagel

    (@big-bagel)

    I think you may be confused by the function’s name (it can be a bit ambiguous); is_admin() returns true if a user is viewing an administration screen and returns false on any front end page.

    Function Reference/is admin

    If you want to check if a user is logged in on the front end, you can use is_user_logged_in().

    Function Reference/is user logged in

    If you want to check to see if a logged in user is an administrator, you can use the current_user_can() function, like so:

    if ( current_user_can( 'administrator' ) ) {
        /* Your code */
    }

    Function Reference/current user can

    You can also check for a capability that only an administrator would have rather than specifically for the administrator role, like so:

    if ( current_user_can( 'update_core' ) ) {
        /* Your code */
    }

    https://codex.www.ads-software.com/Roles_and_Capabilities

    Thread Starter moroandrea

    (@moroandrea)

    Hi Big Bagel,

    I got confused by the function name indeed, but echoing the results of the is_admin() return a null string in either case, and this doesn’t sound normal. Is it?

    Thanks
    Andrea

    It doesn’t actually return a null string. It returns a boolean (a true or false value)… which you can’t echo directly because it contains no characters. You would have to test whether it’s true or false and tell PHP to echo something on each condition. For example:

    if(is_admin() === true) {
         echo 'true';
    }
    else {
         echo 'false';
    }

    The function really is doing exactly what it’s supposed to do. ??

    Thread Starter moroandrea

    (@moroandrea)

    Thank for the hint.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Is_Admin not working on WP 3.3.1’ is closed to new replies.