0

I am not able to get this running. What i commented out is running like it should, but i want to use the switch for specific user roles...

add_action( 'um_members_just_after_name', 'my_members_after_user_name', 10, 1 );
function my_members_after_user_name( $user_id ) {

    $user = new WP_User( $user_id );
    /*
    if( $user->roles[0] == 'um_musiker' ) {
        // update_user_meta( $user_id, 'um_reihung', '40' );
        echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>';  
    }
    */

    if( $user->roles[0] == $value ) {
        switch ( $value ) {
        case "um_musiker":
            echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>';
            break;
        case "um_musiker_bronze":
            echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>';
            break;
        case "um_musiker_silber":
            echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>';
            break;
        }
    }
}
0

You do not need to use pass the $value in a switch statement, Just pass there $user->roles[0] instead of $value and after that your condition.

Add below code and let me know your answer towards it.

add_action( 'um_members_just_after_name', 'my_members_after_user_name', 10, 1 );
function my_members_after_user_name( $user_id ) {

    $user = new WP_User( $user_id );
    /*
    if( $user->roles[0] == 'um_musiker' ) {
        // update_user_meta( $user_id, 'um_reihung', '40' );
        echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>';  
    }
    */


        switch ( $user->roles[0] ) {
        case "um_musiker":
            echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>';
            break;
        case "um_musiker_bronze":
            echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>';
            break;
        case "um_musiker_silber":
            echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>';
            break;
        }

}
  • Hello, i dont need the whole IF statement then - can you pls update your code, then i will mark as solved THX for your help – LovinQuaQua Mar 29 at 9:50
  • removed if condition – Tejas Gajjar Mar 29 at 9:52
1

Checking $user->roles[0] is not an appropriate way to check if a user has a role. Users can have multiple roles, so this needs to be taken into account:

$user  = get_userdata( $user_id );
$roles = $user->roles;

if ( in_array( 'um_musiker', $roles ) {
    echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>';
    return;
}

if ( in_array( 'um_musiker_bronze', $roles ) {
    echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>';
    return;
}

if ( in_array( 'um_musiker_silber', $roles ) {
    echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>';
    return;
}
  • Note that in this case probably a user can only have one role. But yeah, it's a bad habit to get into counting on that. There is an interesting side question of how best to handle the situation where roles are singular. (And confusion about this is why some systems have 'capabilities' instead of (or in addition to) roles. – Loren Rosen Mar 30 at 3:38
  • Roles are supposed to be just collections of capabilities. Using them like this for memberships is ultimately incorrect. Ideally such information would be stored in a different way, like meta, or a custom table. – Jacob Peattie Mar 30 at 4:39
0

I just write new $value = $user->roles[0]; variable and check $user in if condition. I hope it'll help you out. Thanks

add_action( 'um_members_just_after_name', 'my_members_after_user_name', 10, 1 );
function my_members_after_user_name( $user_id ) {

  $user = new WP_User( $user_id );
  /*
  if( $user->roles[0] == 'um_musiker' ) {
    // update_user_meta( $user_id, 'um_reihung', '40' );
    echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>';  
  }
  */

  $value = $user->roles[0];

  if( $value ) {
    switch ( $value ) {
    case "um_musiker":
        echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Kein Status"></i>';
        break;
    case "um_musiker_bronze":
        echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Bronze Status"></i>';
        break;
    case "um_musiker_silber":
        echo '<i class="um-verified um-icon-checkmark-circled um-tip-s" title="Silber Status"></i>';
        break;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.