• Resolved mfrethy

    (@mfrethy)


    Previously I had a function written that would update a users role to member when an administrator would approve a membership.
    Code looked like this:

    add_action('um_after_user_is_approved', 'approved_um_and_wp_role', 99 );
    function approved_um_and_wp_role( $user_id ) {
    	// Set UM role
    	update_user_meta( $user_id, 'role', 'member' );
    	$role = get_user_meta( $user_id, 'role', true);
    
    	// Set WordPress role for same user
    	if ( $role == 'member' ) {
    		$wp_user_object = new WP_User( $user_id );
    		$wp_user_object->set_role( 'volunteer' );
    	} 
    }
    
    // Set user role on deactivate
    add_action('um_after_user_is_inactive', 'deactivated_um_and_wp_role', 99 );
    function deactivated_um_and_wp_role( $user_id ) {
    	// Set UM role
    	update_user_meta( $user_id, 'role', 'deactivated' );
    	$role = get_user_meta( $user_id, 'role', true);
    
    	// Set WordPress role for same user
    	if ( $role == 'deactivated' ) {
    		$wp_user_object = new WP_User( $user_id );
    		$wp_user_object->set_role( 'none' );
    	} 
    }

    With 2.0 however, it no longer cares about the “role” field and everything is dictated by the status and the _capabilities row in user_meta. How can I modify this so that, when I approve a membership it updates their community/user role? I currently have the default role after registration set to “Needs Approval” so that I can categorize potential members. Then I approve their membership and change their role to “Volunteer”. The database entry looks like _capabilities : a:1:{s:12:”um_volunteer”;b:1;}

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter mfrethy

    (@mfrethy)

    Okay, looking through the 2.0 codex and hooks I figured out that this code achieves what I’m looking for:

    add_action( 'um_after_user_is_approved', 'my_after_user_is_approved', 10, 1 );
    function my_after_user_hash_is_changed( $user_id ) {
        $ultimatemember->user->set_role('volunteer');
    }
    Thread Starter mfrethy

    (@mfrethy)

    Well that didn’t exactly work. I’ve set the user first. When I set it dynamically or using a static ID it throws an error.

    Thread Starter mfrethy

    (@mfrethy)

    All of the codex stuff seems to not work in 2.0. It all returns null even when a static number is passed. Oh well. I fixed the problem.

    // Set user role on approved
    add_action('um_after_user_is_approved', 'my_after_user_is_approved', 10, 1 );
    function my_after_user_is_approved ( $user_id ) {
    	// Set UM role
    	$wp_user_object = new WP_User( $user_id );
    	$wp_user_object->set_role( 'um_volunteer' );
    
    }
    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    The author cannot support UM 2.0 in these forums.

    https://wordpress.org/support/topic/read-this-for-version-2-x-support/?view=all

    Please seek support for that version on their own site.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Update Community Role on Approved Membership UM 2.0’ is closed to new replies.