1
add_filter( 'gravityflow_assignee_field_users', 'sh_gravityflow_assignee_field_users', 10, 3 );
function sh_gravityflow_assignee_field_users( $users, $form_id, $field ) {
    $users = array(
        array( 'value' => 'user_id|2', 'text' => 'Joe' ),
        array( 'value' => 'user_id|3', 'text' => 'Jane' ),
    );
    return $users;

}

I want to include the current user ID and user name to be at the top list or first choice in that array field, or better still only output current user iD and user name in the array or drop down list, so if users login his name will only be the option available for selection from the array. if i can get one array or option of the current user i will be good.

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
pandglobal
  • 31
  • 1
  • 9

4 Answers4

0

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.

Snuwerd
  • 459
  • 3
  • 6
  • perfect i want another modification, the codes works, but i want to add a set of if statement to it to show different name based on user meta value. lets say if display name == jerry, return '400'; if Mary then show 600, if Donald then show 340, i will add the sample code in this tread as an answer, but is wrong, it throw error, please help fix it. – pandglobal Nov 23 '19 at 22:44
  • i think you are getting close to what i wanted but then the functions is not working, it still out puts users display name instead of the numbers. I think that part $user2[] is not functioning – pandglobal Nov 24 '19 at 10:51
  • I am doing this on a drop down field type, but i will try it on a single text field, but then lets assume we are working on a simple logic function, normally is user display = mary the normal thing is for $user to return mary, but here instead of it returning mary we will replace mary with 300 , and if user is John we will replace john with 450 – pandglobal Nov 24 '19 at 10:57
  • Is see, edited the post, this should work. Just needed to put the number as the `'text'` in the array. ps. Do you mind marking my answer as correct when its working for you? – Snuwerd Nov 24 '19 at 11:20
  • i have tried it is not working, it just added -1 to the field as default value, nothing else changes... i think using a set of if statements will make the value to change based on user display – pandglobal Nov 24 '19 at 17:14
  • the updated code is not changing.... it returns a static number – pandglobal Nov 25 '19 at 12:03
  • How did I not get notifications from you. Let me take a look. – Snuwerd Nov 25 '19 at 12:11
  • I think the code is fine, you just need to insert the right names and numbers on the `case "":` lines. Right now none of the names in the cases are in the initial array. Also, what number do you want it to return if no number is assigned? Right now it is `-1`. – Snuwerd Nov 25 '19 at 12:14
  • I changed 2 of the names on the `case` lines so they match at least. – Snuwerd Nov 25 '19 at 12:15
  • Well i will try your code too and see if it worked, however i got a code that works perfectly the way i wanted it and i have posted it on the answer section, i appreciate your time, but i want us to start another topic where the code i got can be modified – pandglobal Nov 25 '19 at 14:26
0
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' ),
);
if ($users == jerry) {
return 400;
if ($users == Mary) {
return 600;
if ($users == Donald) {
return 340;

}

pandglobal
  • 31
  • 1
  • 9
  • My mission is that, since we are able to get the code to return current user id and display name, i will also like to stretch it further to allow the code to output a numeric value based on the current user display name. lets say if .$current_user->diplay == jerry return value as 400 – pandglobal Nov 23 '19 at 23:00
  • Is it not possible to edit the original question? If you can just add "update", make it bold and add this under it. – Snuwerd Nov 23 '19 at 23:10
  • Also in this example you dont return the `$users` object anymore, but only a number. Is that correct? Where do you want the number to be? As part of the array that belongs to the user? `array( 'value' => 'user_id|'.$current_user->ID, 'text' => $current_user->display_name, 'number' => 400)` – Snuwerd Nov 23 '19 at 23:18
0
add_filter( 'gform_field_value_bank20_code', 'my_custom_population_shownumber2' );
function my_custom_population_shownumber2( $value ) {
$current_user = wp_get_current_user();
    $users = $current_user->PROCESSOR;
    $users2 = [];
    foreach ($users as $users2) {
        $number = -1;
        switch ($user->text) {
            case "jerry":
                $number = 400;
                break;
            case "Mary":
                $number = 600;
                break;
            case "Donald":
                $number = 340;
                break;
            default:
                break;
        }
        $user['text'] = $number;
        array_unshift($users2, $user);
    }

    return $users2;    
}
pandglobal
  • 31
  • 1
  • 9
  • I tried to remove the other array too and modified the code, it only returns the users meta userpinn which is mary, yet i want to convert jerry to 400 and Donald to 340 @snuwerd – pandglobal Nov 25 '19 at 01:11
  • Lets work on the code above and show me what am not doing right, pls – pandglobal Nov 25 '19 at 01:12
0
add_filter( 'gform_field_value_bank20_code', 'my_custom_population_showcountry' );
function my_custom_population_showcountry( $value ) {
$current_user = wp_get_current_user();
    $users = $current_user->PROCESSOR;

// For the country Afghanistan
if( $current_user->PROCESSOR == 'Afghanistan' )
    $users = ( '500' );

// For the country Albania
if( $current_user->PROCESSOR == 'Albania' )
    $users = ( '600' );

// For the country Algeria
if( $current_user->PROCESSOR == 'Algeria' )
    $users = ( '600' );

// For the country American Samoa
if( $current_user->PROCESSOR == 'American Samoa' )
    $users = ( '600' );


    return $users;    
}

That is the perfect way i got it working with the code above, everything works fine now, Thanks for your time

pandglobal
  • 31
  • 1
  • 9
  • Now since we got it working with the above code, i will like us to add a google api currency rate inside that number column so that the rates will be updated automatically, instead of using manual method to update the rate @snumerd – pandglobal Nov 25 '19 at 14:30
  • Please open a new question for this, this is deviating a lot from the initial question. – Snuwerd Nov 25 '19 at 14:31
  • i have opened another question and here is the link https://stackoverflow.com/questions/59034541/adding-currency-rate-to-varibles – pandglobal Nov 25 '19 at 14:56