List users except current user using wp_dropdown_user?

I am creating a plugin where I need to select other users except me(current user who is logged in).

$user_id = get_current_user_id();

wp_dropdown_users(array(
    'name' => 'user',
    'exclude' => '$user_id,1',
    'show' => 'user_login'
));

is not providing required result. How to overcome this issue?

Related posts

Leave a Reply

1 comment

  1. Your problem is here:

    'exclude' => '$user_id,1',
    

    This will generate the string ‘$user_id,1’, it won’t insert the user ID into your string.

    The reason for this is the difference between ‘$hello’ and “$hello” E.g.

    $hello = 5;
    echo '$hello'; // prints out $hello
    echo "$hello"; // prints out 5
    

    So using double rather than single quotes would fix your issue.

    An even more reliable way

    Use string concatenation to tie them together instead, e.g:

    'exclude' => $user_id.',1',
    

    The . operator joins two strings together, e.g.:

    echo 'Hello there '.$firstname.', my name is '.$myname;
    

    A more generic way

    Use an array, and use implode to generate the string

    $excluded_users = array();
    $excluded_users[] = 1;
    $excluded_users[] = $user_id;
    // .. etc
    'exclude' => implode( ',' , $excluded_users )
    

    I recommend you read up on magic quotes in basic PHP and string handling