How to determine whether a user is an administrator in WordPress

Source: Internet
Author: User

I am writing a small program today. I need to determine whether the role of the specified user is a website administrator. If I know that the user id is 123, I want to know whether the user is an administrator or not.

1. Determine whether the user with the specified id is an administrator

The implementation of this requirement is very simple. Let's take a few lines of code and share the following:

Function ludou_is_administrator ($ user_id ){
$ User = get_userdata ($ user_id );
If (! Empty ($ user-> roles) & in_array ('admin', $ user-> roles ))
Return 1; // It is the administrator.
Else
Return 0; // Non-administrator
}

II. Determine whether the current login user is an administrator

To determine whether the current logon user is an administrator, use the following function:


Function ludou_is_administrator (){
// The wp_get_current_user function is only used in the topic functions. php.
$ CurrentUser = wp_get_current_user ();

If (! Empty ($ currentUser-> roles) & in_array ('admin', $ currentUser-> roles ))
Return 1; // It is the administrator.
Else
Return 0; // Non-administrator
}

3,

This chart shows the levels corresponding to each WordPress user group. We need to determine whether the WordPress user group is an Administrator.

We use level_10. The judgment code is as follows:


If (current_user_can ('level _ 10 ')){
// Add content that meets the administrator requirements
} In this way, users of other levels can be determined by changing the level.

With this, we can make a lot of corresponding functions for each user group.


Supplement:

1. Use current_user_can () to determine

Current_user_can () can be used to determine user Roles based on the permissions of different Roles. Specific user permissions can be found in Roles and Capabilities.
Determine whether the user is an Administrator)

If (current_user_can ('manage _ options ')){

Echo 'the current user is a administrator ';

    }

Determine whether the user is edited (Editor)

If (current_user_can ('Publish _ page ')&&! Current_user_can ('manage _ options ')){

Echo 'the current user is an editor ';

    }

Determine whether the user is the Author (Author)

If (current_user_can ('Publish _ posts ')&&! Current_user_can ('Publish _ page ')){

Echo 'the current user is an author ';

    }

Determine whether the user is a Contributor)

If (current_user_can ('edit _ posts ')&&! Current_user_can ('Publish _ posts ')){

Echo 'the current user is a contributor ';

    }

Determine whether a user is a Subscriber)

If (current_user_can ('read ')&&! Current_user_can ('edit _ posts ')){

Echo 'the current user is a subscriber ';

    }

II. Use $ current_user to judge

$ Current_user is a global variable of WordPress. After a user logs on, the user's role and permission information is displayed.

After executing the init action of WordPress, you can safely use the $ current_user global variable.
Determine whether the login user is the Author (Author) in the template file)

Global $ current_user;

If ($ current_user-> roles [0] = 'author '){

Echo 'the current user is an author ';

    }

In functions. php, determine whether the user is an Author (Author)

Add_action ('init ', 'Check _ user_role ');

Function check_user_role (){

Global $ current_user;

If ($ current_user-> roles [0] = 'author '){

Echo 'the current user is an author ';

        }

    }

Use

Add_action ('init ', 'Check _ user_role ');

It is because all the variables $ current_user are assigned a value only when the init action is executed. To read the content, you must wait until the content is ready. The code of functions. php is executed with init action first. Therefore, writing global $ current_user directly in functions. php cannot obtain user information.

Before checking the user role, you can also check whether the user is logged on.

<? Php

If (is_user_logged_in ()){

// The user has logged on and checked the user role

          }

?>

Simpler method

There is also a more direct method, such as determining whether the current user is an administrator

Global $ current_user;

If (in_array ('admin', $ current_user-> roles )){

Echo 'admin ';

    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.