itsalltech1
Member
Posted 6 months ago #
Hello.
Just updated to WordPress 3.3 beta 3 and noticed that the following code isn't working properly:
<?php get_currentuserinfo() ; global $user_level; if ($user_level >= 2) {?>
The code is supposed to check the current user that is logged in and see if that user is above a level (2), then display a specific link on the page. However, that link is not visible at all - logged in or logged out.
Are there any alternatives to achieving the same idea? Thanks!
User levels have been deprecated. You should check user roles instead:
Roles and Capabilities
If you need to check that the current user has the appropriate privileges, you can use current_user_can():
Function Reference/current user can
From the Codex:
Note: User Levels were introduced in WordPress 1.5, they were replaced by Roles and Capabilities in WordPress 2.0, and were finaly deprecated in WordPress 3.0.
You will want to use the function current_user_can( $capability or $role ).
http://codex.wordpress.org/Function_Reference/current_user_can
Thanks for the report. I have created a ticket for this to be fixed before the final release of 3.3 at http://core.trac.wordpress.org/ticket/19265.
However, as suggested by the two other replies you should update your code use roles and capabilities rather than user levels as that is the correct method for determining privileges.
itsalltech1
Member
Posted 6 months ago #
Thanks guys. I've switched the code to the following:
<?php if (current_user_can('author') || current_user_can('editor') || current_user_can('administrator')) {?>
If you wanted to simplify that slightly, you could instead check for a certain capability that all those roles would share. For example:
<?php if ( current_user_can( 'publish_posts' ) ) { ?>