Support » Fixing WordPress » Checking if the User is Admin

  • Resolved cloudstr210

    (@cloudstr210)


    Hello, I just want to check if the user is admin but I always got error..

    The error is unexpected > on the code..

    <?php if ( ! is_admin() ) {
    
    } else {
    }
    ?>

    I want to put adsense script on it.. But I always got unexpected >

    Thank you.

Viewing 15 replies - 1 through 15 (of 20 total)
  • Does this work? I know you want the code in there, but let’s start with this 🙂

    <?php if ( ! is_admin() ) {
      echo '<p>adsense!</p>';
    }
    ?>

    Does not work too.. This is the error

    Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in /home/xxx/public_html/xxxx/wp-content/themes/xxx/single.php on line 6

    What exactly is on line 6?

    esmi

    (@esmi)

    Forum Moderator

    There’s nothing wrong with Ipstenu’s code.

    I tried this code.. and have no error, but the adsense is still showing even the admin is login..

    <?php if (!is_admin()) { ?>
    <?php include('adsense.php'); ?>
    <?php } ?>

    @danhgilmore

    Its the last code of the adsense code.

    @esmi

    Thanks for the reply.. I know theres nothing wrong in it but its not working for me..

    I tried this too..

    <?php if (!is_admin()) { ?>
    echo ‘<?php include(‘adsense.php’); ?>’;
    <?php } ?>

    But it seem that the function !is_admin is not working?

    esmi

    (@esmi)

    Forum Moderator

    That code is hopelessly broken. Try Ipstenu’s code again and, this time, make sure that you’re adding it to the right place in your template file.

    …I just want to check if the user is admin…

    Are you trying to check if an administration screen is being shown or if the current user is an administrator? If it’s the latter, then you want something like this:

    if ( current_user_can( 'manage_options' ) ) {
        /* A user with admin privileges */
    } else {
        /* A user without admin privileges */
    }

    Function Reference/current user can

    Yup, I need to check if the current user is admin..

    Do i need to put <?php?

    could you give me sample with adsense dummy code on it.. thanks!

    Yeah.. is_admin() checks if you’re on an admin page.

    is_admin()

    Try

    <?php if (current_user_can( 'manage_options' )) {
            include('adsense.php');
     } ?>

    This allows you to choose any role by checking if the current_user_can do any particular task. Check the table at the bottom of this link and you’ll be able to pick any role by selecting a capability that the role you want can do, but not the roles below.

    Roles and Capabilities << WordPress Codex

    One more thing.. if you are using more than one PHP command they can all be included in the same <?php ?> bracket set.

    Happy Coding…

    whiteatom

    cloudstr210 – What I was asking was for you to try without your adsense code.

    I use is_admin() all the time to show things to my admin account and not my visitors, on the frontend of my site.

    So let’s go back to the beginning.

    Does EXACTLY this work?

    <?php if ( ! is_admin() ) {
      echo '<p>adsense!</p>';
    }
    ?>

    DO NOT put your own code in 🙂 We’ll get there, but you have to step through things in order when debugging.

    But, is_admin just checks if the request was for an administration page. From the inline documentation:

    Does not inform on whether the user is an admin! Use capability checks to tell if the user should be accessing a section or not.

    Comment: It cannot, and will never be, a boon to users in this forum to see three folks disagree on how to do things…Shall I believe Emsi (mod), IpStenu (mod), or Big Bagel (?)?

    Am I alone in this sentiment?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Checking if the User is Admin’ is closed to new replies.