WordPress Two Development Tutorials notes: Multi-site user data sharing

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

Recently, a customer asked small V to help their company site to do an English version, and the Chinese version and English version of the two site user data can be interoperable and does not affect the normal operation of the site. OK, start the egg pain toss, first read the official WordPress document: "WordPress data structure Map" found that the User data table WordPress (Wp_users and Wp_usermeta) is relatively independent, Then we can directly share the user database of two sites without considering the impact of user data on other data in two sites. The following small V to say the solution:

Suppose 11545.html "> we have two wordpress sites, one for cn.v7v3.com (data table prefix is v7v3cn_) and another for en.v7v3.com (datasheet prefix is v7v3en_), We will cn.v7v3.com as the main site, will en.v7v3.com as the main station of an English version of the secondary site.

First open the En.v7v3.com wp_config.php file and add the following code:

Define (' custom_user_table ', ' v7v3cn_users '); V7v3cn_ database prefix for primary site

Define (' custom_user_meta_table ', ' V7v3cn_usermeta ');

After adding the above code, the user data between the two sites realizes the initial interoperability, but if you use the master station to log in to the secondary site, you will be prompted not to have sufficient permissions. The reason is because the master station store user right value is V7V3CN to start with, take the master station's administrator as an example, the administrator user ID is 1, the role is administrator, then the table has such a record:

User_id->1, Meta_key->v7v3cn_capabilities, meta_value->a:1:{s:13: "Administrator"; s:1: "1";}

The English side of the database has not been v7v3en_ the beginning of the permission value record, so the lack of permission to prompt. The workaround is to run the SQL statement:

Add a permission record value to the database that starts with V7v3en_

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";} ');

This only allows a user ID of 1 administrators to log on to both sites, if the creation of new users, whether administrators or ordinary users will be prompted insufficient permissions, the solution is in the user's registration hook to add a SQL database operation:

Set the prefix for the master station, and other sites share the user datasheet for the site

$main _prefix = ' v7v3cn_ ';

Set the prefix for the child station, prefixed by V7v3en

$addi _prefixs = Array (' V7v3en_ ');

Add functionality to user registered hooks

Add_action (' User_register ', ' dup_capabilities ');

function dup_capabilities ($user _id) {

Global $main _prefix, $addi _prefixs;

Gets the value of the user right because the values for different roles are different

if ($cap _val = Get_user_meta ($user _id, $main _prefix. ' Capabilities ', true)} {if (count ($addi _prefixs) > 0) {Forea CH ($addi _prefixs as $prefix) {Add_user_meta ($user _id, $prefix. ' Capabilities ', $cap _val, True);} } }

Make the above code a widget or a functions.php file added to the subject, and it will be done once and for all. Small v in the local test using the wordpress3.4 version, there is no problem, but in the customer's site to use the error, check the reason, the original customer side of the site with the wordpress3.5 version, The code in 3.5 is not quite the same as 3.4, so 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) {

Here to set the data table prefix, do not divide the main station child station, all written on it.

$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, $pref Ix. ' capabilities ', $cap _val); } } }

This article source: http://v7v3.com/wpjiaocheng/201307112.html reprint Please specify the source! Thank you for your cooperation.

Finally, the last piece of code is the same as the above code, now two site user data can be completely interoperable. PS: The above method applies only to two WordPress sites installed on one server and sharing one data. )

Related Article

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.