Skip to content

Instantly share code, notes, and snippets.

@timba64
Forked from carlodaniele/kinsta-share-users.php
Created April 24, 2019 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timba64/adaed0520687362d711e7506844dfa62 to your computer and use it in GitHub Desktop.
Save timba64/adaed0520687362d711e7506844dfa62 to your computer and use it in GitHub Desktop.
A plugin to share users and usermeta tables between independent WordPress installations. This plugin requires CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE defined into wp-config file
<?php
/**
* @package Kinsta_Share_Users
* @version 1.0
*/
/*
Plugin Name: Kinsta Share Users
Plugin URI: http://wordpress.org/extend/plugins/#
Description: This is an example plugin for Kinsta blog readers
Author: Carlo Daniele
Version: 1.0
Author URI: http://carlodaniele.it/en/
*/
/**
* Duplicate {$pref}_capabilities and {$pref}_user_level rows in {$pref}_usermeta table
*
* @param int $user_id The user ID.
* @param string $role The new role.
* @param array $old_roles An array of the user's previous roles.
*
* @link https://developer.wordpress.org/reference/hooks/set_user_role/
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/set_user_role
*
*/
function ksu_save_role( $user_id, $role ) {
// Site 1
// Change value if needed
$prefix_1 = 'first_';
// Site 2 prefix
// Change value if needed
$prefix_2 = 'second_';
$caps = get_user_meta( $user_id, $prefix_1 . 'capabilities', true );
$level = get_user_meta( $user_id, $prefix_1 . 'user_level', true );
if ( $caps ){
update_user_meta( $user_id, $prefix_2 . 'capabilities', $caps );
}
if ( $level ){
update_user_meta( $user_id, $prefix_2 . 'user_level', $level );
}
}
add_action( 'set_user_role', 'ksu_save_role', 10, 2 );
@timba64
Copy link
Author

timba64 commented Apr 24, 2019

About this plugin you can read in https://kinsta.com/blog/share-logins-wordpress/#duplicate-caps-and-levels
If you have working site, you have many dates in datebase, you should see field wp_user_roles in wp_options table. In all wp_options tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment