WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Checking if the User is Admin (21 posts)

  1. cloudstr210
    Member
    Posted 2 years ago #

    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.

  2. 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>';
    }
    ?>
  3. cloudstr210
    Member
    Posted 2 years ago #

    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

  4. danhgilmore
    Member
    Posted 2 years ago #

    What exactly is on line 6?

  5. esmi
    Forum Moderator
    Posted 2 years ago #

    There's nothing wrong with Ipstenu's code.

  6. cloudstr210
    Member
    Posted 2 years ago #

    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 } ?>
  7. cloudstr210
    Member
    Posted 2 years ago #

    @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..

  8. cloudstr210
    Member
    Posted 2 years ago #

    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?

  9. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  10. Big Bagel
    Member
    Posted 2 years ago #

    ...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

  11. cloudstr210
    Member
    Posted 2 years ago #

    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!

  12. whiteatom
    Member
    Posted 2 years ago #

    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

  13. whiteatom
    Member
    Posted 2 years ago #

    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

  14. 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.

  15. Big Bagel
    Member
    Posted 2 years ago #

    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.
  16. Pioneer Valley Web Design
    Member
    Posted 2 years ago #

    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?

  17. cloudstr210
    Member
    Posted 2 years ago #

    Thank you.. Problem solved!

  18. Big Bagel - I know, and yet... Anyway, I'm of the whatever works school ;) if (current_user_can( 'manage_options' )) is better, you're right, but if it works, I don't knock it. And either way? We needed to pull the adsense CODE out of the equation to figure out WHAT wasn't working ;)

    SwansonPhotos - WordPress. Four people, six opinions ;) It's not a big deal really. Esmi and I wanted to see if his stuff worked without the adsense. We're psychic, you see, and she knew my thought was that it wasn't the if() code that was throwing up, but the adsense code ;)

  19. Big Bagel
    Member
    Posted 2 years ago #

    I completely agree with using whatever works and pulling the adsense bit out while testing. :)

    However, if the goal was to hide the adsense code on the front end from administrators, if ( ! is_admin() ) wouldn't work. It would always evaluate as true for any front end page regardless of the role of the current user.

    At the very least, whatever code cloudstr210 is now using resolved his problem, which is awesome sauce. :)

  20. Brlocky
    Member
    Posted 2 years ago #

    Just to clarify the

    is_admin() function is used to check if you are in Admin area or in Front area.

    To check if the user is an Admin you should check his permissions !!

    hope this can help some one !

  21. tactiq
    Member
    Posted 1 year ago #

    I think is_super_admin() will do the trick

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.