• Resolved macca_andy47

    (@macca_andy47)


    Hi all,

    Someone previously helped me with some code to assign someone a UM role based on their age when they registered on my site.

    Im trying to update that code so it uses the account update hook instead. However, I can’t seem to get it to work. The only code I’ve changed is the first few lines after i found a hook on the support pages. Im not very good with PHP, so just wondering if someone was able to help me tweak it so it works?

    Here’s the page I got the hook from:

    https://docs.ultimatemember.com/article/1290-umuserafterupdatingprofile

    Here’s the code

    // Account update hook //

    add_action( ‘um_user_after_updating_profile’, ‘my_user_after_updating_profile’, 10, 1 );
    function my_user_after_updating_profile($this_id, $submitted ) {

    // code used from registration script…

    $age = $submitted[‘age’];

    $level = $submitted[‘player_level’];

    $user = get_user_by(’email’,$submitted[‘user_email’]);

    // remove um_player-sponsorship-application

    $u = new WP_User( $user->ID );
    // Remove role
    $u->remove_role( ‘um_player-sponsorship-international’ );

    if($age>=16 && $level==’International’)
    {
    // Add role
    $u->add_role( ‘um_player-sponsorship-international-16-or-over’ );
    um_fetch_user($user->ID);
    }

    else

    if($age<=15 && $level==’International’)
    {
    // Add role
    $u->add_role( ‘um_player-sponsorship-international-15-or-under’ );
    um_fetch_user($user->ID);
    }

    else

    if($age<15 && $level!=’International’)
    {
    // Add role
    $u->add_role( ‘um_player-sponsorship-level-1′ );
    um_fetch_user($user->ID);

    }
    else if($age>=15 && $age<=17 && $level!=’International’)
    {

    $u->add_role( ‘um_player-sponsorship-level-2′ );
    um_fetch_user($user->ID);

    }

    else if($age>=18 && $age<=23 && $level!=’International’)
    {

    $u->add_role( ‘um_player-sponsorship-level-3′ );
    um_fetch_user($user->ID);

    }

    else if($age>24 && $level!=’International’)
    {
    $u->add_role( ‘um_player-sponsorship-level-4’ );

    um_fetch_user($user->ID);

    }

    }`

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Aswin Giri

    (@aswingiri)

    Hello @macca_andy47

    Please try UM()->roles()->set_role( $user_id, 'your-role-id' ) ); instead of $u->add_role( ‘um_player-sponsorship-level-4’ ); and see if that solves your issue.

    Thread Starter macca_andy47

    (@macca_andy47)

    That doesn’t seem to be working. Is this the correct hook for if someone updates their profile? https://docs.ultimatemember.com/article/1290-umuserafterupdatingprofile

    • This reply was modified 2 years, 4 months ago by macca_andy47.

    @macca_andy47

    You can try this code snippet:

    // Profle update hook //
    
    add_action( 'um_user_after_updating_profile', 'um_user_after_updating_profile_custom', 10, 3 );
    
    function um_user_after_updating_profile_custom( $to_update, $user_id, $args ) {       
    
        if( !empty( $to_update['player_level'] ) && !empty( $to_update['age'] )) {
    
            $new_role = '';
            $age = (int)$to_update['age'];
    
            if( $to_update['player_level'] == 'International' ) {
    
                if ( $age >= 16 ) $new_role = 'um_player-sponsorship-international-16-or-over';        
                else              $new_role = 'um_player-sponsorship-international-15-or-under';
    
            } else {
    
                if ( $age < 15 )                $new_role = 'um_player-sponsorship-level-1';        
                if ( $age >= 15 && $age <= 17 ) $new_role = 'um_player-sponsorship-level-2';        
                if ( $age >= 18 && $age <= 23 ) $new_role = 'um_player-sponsorship-level-3';        
                if ( $age >= 24 )               $new_role = 'um_player-sponsorship-level-4';
            }
    
            if( !empty( $new_role )) UM()->roles()->set_role( $user_id, $new_role );
        }
    }
    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hey there!

    This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. ??

    Regards,

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘User role assignment via PHP’ is closed to new replies.