See if wp_get_current_user()
works to get the user in your function?
$current_user = wp_get_current_user();
// $current_user->user_login
// $current_user->user_email
// $current_user->user_firstname
// $current_user->user_lastname
// $current_user->display_name
// $current_user->ID
From https://codex.wordpress.org/Function_Reference/wp_get_current_user
So it will probably look like this:
add_filter( 'gravityflow_assignee_field_users', 'sh_gravityflow_assignee_field_users', 10, 3 );
function sh_gravityflow_assignee_field_users( $users, $form_id, $field ) {
$current_user = wp_get_current_user();
$users = array(
array( 'value' => 'user_id|'.$current_user->ID, 'text' => $current_user->display_name),
array( 'value' => 'user_id|2', 'text' => 'Joe' ),
array( 'value' => 'user_id|3', 'text' => 'Jane' ),
);
return $users;
}
Update
Had a mistake in my code.
Update
I don't know exactly what you need (which is not a good thing, I shouldn't be guessing). But this may be what you need. Your example can't be correct, because you were now returning a number instead of the $users
array.
For each user in the list, the code below changes what is in the field 'text'
with a specific number depending on the user's display name.
add_filter( 'gravityflow_assignee_field_users', 'sh_gravityflow_assignee_field_users', 10, 3 );
function sh_gravityflow_assignee_field_users( $users, $form_id, $field ) {
$current_user = wp_get_current_user();
$users = array(
array( 'value' => 'user_id|'.$current_user->ID, 'text' => $current_user->display_name),
array( 'value' => 'user_id|2', 'text' => 'Joe' ),
array( 'value' => 'user_id|3', 'text' => 'Jane' ),
);
$users2 = [];
foreach ($users as $user) {
$number = -1;
switch ($user['text']) {
case "Joe":
$number = 400;
break;
case "Jane":
$number = 600;
break;
case "Donald":
$number = 340;
break;
default:
break;
}
$user['text'] = $number;
array_unshift($users2, $user);
}
return $users2;
}
Update
My apologies, I was treating $user as an object instead of an associative array, so $user->text
has to be $user['text']
. Changed the code.