WordPress.org

Ready to get started?Download WordPress

Forums

Multisite user update hook (4 posts)

  1. jjones7
    Member
    Posted 1 year ago #

    Hi I am looking for a hook to use on a multisite install that will let me hook into when any user across the network has their role changed.

    I need to know which users role was changed, which blog the role was changed within and what the role was changed to.

    The nearest thing I can find is:

    'set_user_role' hook, but I cant get it to return what I want without hacking the core.. and I am not sure it will capture a role change in all circumstances even if I went with this solution..

    Does anyone have any suggestions?

    Cheers

  2. jjones7
    Member
    Posted 1 year ago #

    Hi, just for further info this is the hack I have done..

    In the users.php file I have added in the blog id of the blog being changed to the $user->set_role() function. This in turn has allowed me to add this blog id into the 'set_user_role' action hook that sits within that function.

    Obviously really would not recommend doing this as will have to hack the core every time we upgrade, but would appreciate help on a more elegant solution,. or perhaps put forward as a suggested change to the core?

  3. I would email this to the WP-hackers list, actually, since that's a fairly advanced thing... Is there even anything single site that tracks user role changes like that?

  4. jjones7
    Member
    Posted 1 year ago #

    In case your interested thought I would share...

    So far I have found three action hooks that must be used to get a complete picture of user role changes across the an MU setup.

    2 pass the info I need.

    1./ The 'edit_user_profile' hook i the user-edit file. The object returned is full of goodies and you can get the changed sites id without doing any hacks.
    do_action( 'edit_user_profile', $profileuser );

    2./ The 'remove_user_from_blog' hook also provides the info required.
    do_action('remove_user_from_blog', $user_id, $blog_id);

    So if we get the 'set_user_role' hook passing some some good info then you can globally track what all the user roles are doing across all sites (without doing my nasty hack)

    in 'users' line 114

    //jons modification//
                    global $blog_id;
                    $initiating_user = get_current_user_id( );
    		$user = new WP_User($id);
    		$user->set_role($_REQUEST['new_role'] , $blog_id, $initiating_user );

    in capabilities 747:

    function set_role( $role,  $blog_id, $initiating_user ) {
    		foreach ( (array) $this->roles as $oldrole )
    			unset( $this->caps[$oldrole] );
    
    		if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
    			return;
    
    		if ( !empty( $role ) ) {
    			$this->caps[$role] = true;
    			$this->roles = array( $role => true );
    		} else {
    			$this->roles = false;
    		}
    		update_user_meta( $this->ID, $this->cap_key, $this->caps );
    		$this->get_role_caps();
    		$this->update_user_level_from_caps();
    		do_action( 'set_user_role', $this->ID, $role,  $blog_id,  $initiating_user);
    	}

    If you find any other places, please let me know :-)

    Thanks

    Jon

Topic Closed

This topic has been closed to new replies.

About this Topic