Recently, a customer asked Mr. V to make an English version for their company's website, and required that the user data of both Chinese and English versions be interconnected without affecting the normal operation of the site. Well, I started to get confused. I first read the official wordpress document: wordpress data structure diagram, and found that wordpress user data tables (wp_users and wp_usermeta) are relatively independent, then we can directly share the user databases of the two sites, without the impact of user data on other data of the two sites. The following section describes the solution:
Suppose we have two wordpress sites: cn.v7v3.com (data table prefix is v7v3cn _) and en.v7v3.com (data table prefix is v7v3en _). We use cn.v7v3.com as the main site, use en.v7v3.com as an English version of the site.
First open the wp_config.php file of en.v7v3.com and add the following code:
Define ('custom_user_table ', 'v7v3cn _ users'); // The database prefix of the master site v7v3cn _
Define ('custom_user_meta_table ', 'v7v3cn _ usermeta ');
After the code is added, the user data between the two sites is initially interconnected. However, if you log on to the secondary site as a user on the primary site, the system prompts that you do not have sufficient permissions. The reason is that the user permission value stored on the master site starts with v7v3cn. Taking the master site administrator as an example, if the administrator user ID is 1 and the role is administrator, the table has such a record:
User_id-> 1, meta_key-> v7v3cn_capabilities, meta_value-> a: 1: {s: 13: "administrator"; s: 1: "1 ";}
However, the database of the English subsite does not have a permission value record starting with v7v3en _, which leads to insufficient permissions. The solution is to run the SQL statement:
// Add the permission record value starting with v7v3en _ to the database
Insert into 'dbname '. 'wp _ usermeta' ('umeta _ id', 'user _ id', 'meta _ key', 'meta _ value') VALUES (NULL, '1 ', 'v7v3en _ capabilities ', 'a: 1: {s: 13: "administrator"; s: 1: "1 ";}');
In this way, only the Administrator with the user ID 1 can log on to these two websites. If a new user is created, both administrator and common user will be prompted that the permissions are insufficient, the solution is to add an SQL database operation in the user registration HOOK:
// Set the prefix of the master site. Other Websites share the user data table of the website.
$ Main_prefix = 'v7v3cn _';
// Set the prefix of the sub-site. The prefix is v7v3en.
$ Addi_prefixs = array ('v7v3en _');
// Add the function to the user registration hook
Add_action ('user _ register ', 'dup _ capabilities ');
Function dup_capabilities ($ user_id ){
Global $ main_prefix, $ addi_prefixs;
// Obtain the value of this user permission because the values of different roles are different.
If ($ cap_val = get_user_meta ($ user_id, $ main_prefix. 'capabilities ', true) {if (count ($ addi_prefixs)> 0) {foreach ($ addi_prefixs as $ prefix) {add_user_meta ($ user_id, $ prefix. 'capabilities ', $ cap_val, true );}}}}
Make the above code into a small plug-in or add it to the functions. php file of the topic, so that it will be done once and for all. Small V used wordpress3.4 for local testing. No problem occurred, but an error was reported when using it on the customer's site. I checked the cause, the customer's site used wordpress3.5. The code in 3.5 is not the same as that in 3.4. Therefore, use the following code for version 3.5 or later:
Add_action ('user _ register ', 'dup _ capabilities ');
Add_action ('profile _ Update', 'dup _ capabilities ');
Function dup_capabilities ($ user_id ){
// Set the data table prefix here, regardless of the main station or sub-station. Just write it all.
$ Prefixs = array ('v7v3cn _ ', 'v7v3en _'); global $ table_prefix; $ cap_val = get_user_meta ($ user_id, $ table_prefix. 'capabilities ', true); if (! Empty ($ cap_val) {foreach ($ prefixs as $ prefix) {if ($ prefix! = $ Table_prefix) update_user_meta ($ user_id, $ prefix. 'capabilities ', $ cap_val );}}}
Source of this article: http://v7v3.com/wpjiaocheng/201307112.html reprint please indicate the source! Thank you for your cooperation
The last code is the same as the above Code. Now the user data of the two sites can be fully interconnected. (PS: the above method only applies when two wordpress sites are installed on one server and share one data .)