Recently I am working on WordPress blogs in the same domain and want the user who registers on one to be able to log into the other without registering again. I have installed separate WordPress using the subdomain and I wanted to synchronize use data in all installation of WordPress.It means if a user logged into one installation, the user can access logged in information of other WordPress.
So Let’s understand how its possible in wordpress.
First of all, for that you need to have the same database for every WordPress installation with the different prefix.
NOTE: All installations of WordPress must have the same version.
Now, Open your second wordpress wp-config.php
file and add following constant into it.
1 2 3 4 | define('CUSTOM_USER_TABLE', 'wp_users'); define('CUSTOM_USER_META_TABLE', 'wp_usermeta'); |
Here wp_
is prefix of main(first) WordPress installation, you can change it to your own prefix.
Well, Second WordPress should have different table prefix than the first one.you can change by placing below line of code.
1 2 3 | $table_prefix = 'wp2_'; |
However, you need to have same DB Connection information, AUTH_KEY, SECURE_AUTH_KEY, LOGGED_IN_KEY and NONCE_KEY as the first WordPress config file so each installation can authenticate your user login detail correctly across the different installation.
That’s it.now all of the WordPress installations are sharing the same user tables.But when you try to log in from other WordPress installation using main WordPress installation’s user detail, you will be logged into WordPress but not able to access wp-admin and get the error message ’You do not have sufficient permissions to access this page.‘.
Problem:
If you get error message “you do not have sufficient permission to access”,that means when it is trying to check user capability using the defined table prefix and which is not matched with the user meta of main WordPress installation.
Read: Post Autosave Interval in WordPress
This is because wordpress core file checking user capabilities and function for same is into wp-include/capabilities.php
.Let’s see the function:
1 2 3 4 5 6 7 8 9 10 11 | function _init_caps() { global $wpdb; $this->cap_key = $wpdb->prefix . 'capabilities'; $this->caps = &$this->{$this->cap_key}; if ( ! is_array( $this->caps ) ) $this->caps = array(); $this->get_role_caps(); } |
Here In above code, you will get $wpdb->prefix = your WordPress table prefix, but we are trying to access user and user meta table of main WordPress installation. That’s why its giving error.
Solution:
A solution of above issue is we manually customize core file of WordPress so replace the following line into function _init_caps
.
1 2 3 4 | // $this->cap_key = $wpdb->prefix . 'capabilities'; $this->cap_key = 'wp_capabilities'; |
But Do you think it is feasible solution?
No, Its not a feasible solution because you will have to change this code in every WordPress update..So here I come with the solution of the problem you get above.I will show you action hook which will update user capabilities into user meta table.
Read about:To get Current URL in wordpress
Above action hook will insert the record for other WordPress installation, you can go to your user meta table and see the meta_key $wpdb->prefix.’capabilities’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | add_action('user_register', 'update_user_meta_capability', 15, 2); add_action( 'profile_update', 'update_user_meta_capability' ); function update_user_meta_capability($user_id) { global $wpdb; $other_prefix='wp_'; if(is_object($user_id)) { $userID = $user_id->ID; } else { $userID = $user_id; } $meta =get_user_meta($userID ,$wpdb->prefix.'capabilities',true); if($meta) { $other_meta = get_user_meta($userID ,$other_prefix.'capabilities',true); if($other_meta ) { update_user_meta($userID ,$other_prefix.'capabilities',$other_meta ); } else { update_user_meta($userID ,$other_prefix.'capabilities',$meta ); } } else { $other_meta = get_user_meta($userID ,$other_prefix.'capabilities',true); if($other_meta ) { update_user_meta($userID ,$wpdb->prefix.'capabilities',$other_meta ); } } } |
You need to place above code into every WordPress theme’s function.php file.
I’m hoping that you’ve found this article helpful. Don’t hesitate to use this code in your WordPress projects and post your comments if you need help. As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.