Support » Plugin: CiviCRM WordPress Member Sync » Several websites sync

  • Resolved tutolex

    (@tutolex)


    Hi,

    I want to share users between different wordpress installations. I managed to do this pretty well in wordpress using this tutorial :
    https://kinsta.com/blog/share-logins-wordpress/

    So whenever an admin updates the role of a user by hand in the first website in wordpress, the user role is also updated in the second website.

    The problem comes when the admin changes the status of the user directly inside CiviCRM, for example from “Pending” to “New” : the wordpress user role is updated on the first site but not in the second.

    The way this sync is done is by using the following code in a plugin :

    
    function ksu_save_role( $user_id, $role ) {
    
    	// Site 1
    	// Change value if needed
    	$prefix_1 = 'wpeu_';
    	
    	// Site 2 prefix
    	// Change value if needed
    	$prefix_2 = 'wpbe_';
    	
    	$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 );

    (Basically adds a usermeta info with the prefix of the second website in the database when ‘set_user_role’ is triggered)

    So after looking inside your plugin, as it doesn’t use the same trigger (set_user_role), I thought I could add the same action using one action that you say is here to warn other plugins that an update has been perfomed :

    		/**
    		 * Let other plugins know that a user's role has been changed.
    		 *
    		 * @param object $user The WordPress user object.
    		 * @param string $new_role The new role that the user has.
    		 * @param string $old_role The role that the user had before.
    		 */
    		do_action( 'civi_wp_member_sync_set_role', $user, $new_role, $old_role );

    So I added the following line of code, made a prayer, but it didn’t work :

    add_action( 'civi_wp_member_sync_set_role', 'ksu_save_role');

    So the code becomes :

    function ksu_save_role( $user_id, $role ) {
    
    	// Site 1
    	// Change value if needed
    	$prefix_1 = 'wpeu_';
    	
    	// Site 2 prefix
    	// Change value if needed
    	$prefix_2 = 'wpbe_';
    	
    	$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 );
    add_action( 'civi_wp_member_sync_set_role', 'ksu_save_role');

    I tried with and without ,12,2 and it just doen’t work.

    I am not that good in wp development and I guess thare might be a pretty basic concept that I’m missing here …

    Could you help me ?

    Thanks so much 🙂

    • This topic was modified 4 months ago by tutolex.
    • This topic was modified 4 months ago by tutolex.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.