WordPress judges user roles and permission levels and integrates databases, causing no background logon permission

Source: Internet
Author: User
Tags install wordpress wordpress database

WordPress judges user roles and permissions

In fact, WordPress user roles and permissions are rarely used. For common WordPress bloggers, this function may not be very commonly used, but for open registration WordPress, this function may be used. Zfan also needs to integrate and synchronize user data. Yesterday, zfan posted a blog titled "WordPress database integration causes no backend logon permission solution". this involves the permissions of WordPress users, so here I have compiled a comprehensive article for you to learn more.

First, let's take a look at the permissions of different WordPress users and roles. By default, WordPress has five roles (administrator, editor, author, contributor, and subscriber) and 11 permissions (level_0 ~ Level_10), where:

The administrator has the highest permissions (level_0 ~ Level_10)

Followed by level_0 ~ Level_7)

Then the author (level_0 ~ Level_2)

Then the author (level_0 ~ Level_1)

Last subscriber (level_0)

In the database, the permission field is located in the meta_value in the wp_usermeta table of the database. Therefore, this judgment function can be written as follows:


<? Php global $ wpdb; // display the administrator $ administrators = $ wpdb-> get_col ("SELECT user_id FROM $ wpdb-> usermeta WHERE meta_key = 'WP _ user_level 'AND meta_value = 10 order by user_id "); // display the administrator, edit $ editors = $ wpdb-> get_col ("SELECT user_id FROM $ wpdb-> usermeta WHERE meta_key = 'WP _ user_level 'AND meta_value> = 7 order by user_id "); // Display administrator, edit, author $ authors = $ wpdb-> get_col ("SELECT user_id FROM $ wpdb-> usermeta WHERE meta_key = 'WP _ user_level 'AND meta_value> = 2 ORDER BY user_id "); // Display administrators, editors, authors, AND contributors $ contributors = $ wpdb-> get_col ("SELECT user_id FROM $ wpdb-> usermeta WHERE meta_key = 'WP _ user_level 'AND meta_value> = 1 order by user_id "); // Display administrators, editors, authors, contributors, subscribers $ subscribers = $ wpdb-> get_col ("SELECT user_id FROM $ wpdb-> usermeta WHERE meta_key = 'WP _ user_level 'AND meta_value> = 0 order by user_id "); // display the display name foreach ($ administrators as $ administrator) {echo get_the_author_meta ('display _ name', $ administrator ). '<br/> ';}



In the above example, only the administrator users with the highest permissions are Output. Let's do it by yourself! The related permissions and roles are described in the annotations.




WordPress database integration causes no backend logon permission solution

I 've been tossing over wordpress recently and found many problems. I recorded a problem and solution about WordPress database member data integration in my blog!

A problem occurs during database synchronization during integration today, that is, when a WordPress user logs on to the background and prompts "you do not have sufficient permissions to access this page". In fact, this situation often occurs, sometimes, when I modified the WordPress database, especially when I modified the WordPress user ID, I posted a post titled "how to modify the WordPress user ID" not long ago. In fact, this may happen if I don't pay attention to it.

This situation is actually very simple, as long as you understand what the role of some WordPress data tables is, you will understand, WordPress is used to store user information mainly has two tables: wp_users, wp_usermeta (wp _ is the prefix of the data table selected by the user to install WordPress). The user mainly stores the basic information of the user, and the usermeta is the permission configuration of the user to store. The two tables wp_posts and wp_comments are also associated with the user data, such as the author of the article and comment information. Therefore, if you only modify the WordPress user ID, please refer to the article "how to modify the WordPress user ID.

Next, let's talk about the theme of zfan. After logging on, the system prompts:

You do not have sufficient permissions to access this page. (You do not have sufficient permissions to access this page)

User permissions are stored in the wp_usermeta table. The value of this permission starts with the wp prefix. Take the first administrator of the site as an example. The administrator user ID is 1 and the role is administrator, the table has a record similar to this (as shown in the following figure: the last and second Records)

User_id-> 1, meta_key-> wp_capabilities, meta_value-> a: 1: {s: 13: "administrator"; s: 1: "1 ";}


There is also the last data record of the screenshot, which is also essential, which is WordPress's user-level permission settings.

The screenshot contains administrator data. However, when the user data is used as user data, an ordinary user cannot use the administrator permission. Generally, the user can be subscribed, similarly, we only need to write two examples based on the above method!

User_id-> 1, meta_key-> wp_capabilities, meta_value-> a: 1: {s: 10: "subscriber"; s: 1: "1 ";}

User_id-> 1, meta_key-> wp_user_level, meta_value-> 0

// Run the following SQL statement in the database to INSERT a user. Remember to see the INSERT INTO wp_users SET ID = 'User ID', user_login = 'username ', user_email = 'User mailboxes ', user_pass = 'User password' insert into wp_usermeta SET user_id = 'User ID', meta_key = 'WP _ capabilities', meta_value = 'A: 1: {s: 10: "subscriber"; s: 1: "1";} 'Insert INTO wp_usermeta SET user_id = 'User ID', meta_key = 'WP _ user_level ', meta_value = '0'


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.