• Hi. I need to create a multiselect field where allowed user can select one or more user belonging only to a specific WP role.

    How is it possible?

    Best regards

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

    (@mansurahamed)

    Hi @french150,

    You can use our choice callback function feature to write a custom function to populate the multiselect fields with user infomations. Please see our docs on this here https://docs.ultimatemember.com/article/1539-choices-callback-feature-in-um-2-1

    Thread Starter french150

    (@french150)

    Hi, I supposed. But how can I create a custom function to populate a dynamic list of all the user(s) belonging to a specific role?

    Can work this function where “operatore” is the name of the WP role?

    Please consider that I want display only the name of users.

    <?php
    
    $args = array(
        'role'    => 'um_operatore',
        'orderby' => 'user_nicename',
        'order'   => 'ASC'
    );
    $users = get_users( $args );
    
    echo '<ul>';
    foreach ( $users as $user ) {
        echo '<li>' . esc_html( $user->display_name ) . '[' . esc_html( $user->user_email ) . ']</li>';
    }
    echo '</ul>';
    
    ?>

    Best regards

    Plugin Support mansurahamed

    (@mansurahamed)

    <?php
    
    function return_my_username_dropdown(){
    $args = array(
        'role'    => 'um_operatore',
        'orderby' => 'user_nicename',
        'order'   => 'ASC'
    );
    $users = get_users( $args );
    
    
    $usernames = array();
    foreach ( $users as $user ) {
       $usernames[] = $user->display_name;
    }
    return $usernames;
    
    }
    ?>

    You can then use this function name “return_my_username_dropdown” as your callback function.

    Thread Starter french150

    (@french150)

    Hi, I think I’ll add this code by Code Snippets plugin, ok?

    So do i have to write in the function “um_operatore” where the role is named “operatore”?

    For the last how can I edit this function to select more than one user from more WP roles? In this case I’ll use multi select field instead of dropdown to select more users

    Best regards

    • This reply was modified 11 months, 2 weeks ago by french150.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to create a multiselect field to select user from a specific WP role’ is closed to new replies.