Choices Callback – NOT WORKING in V2.0.44 and higher
-
Choices Callback does not work in newer versions – 2.0.44 or higher.
The choices are properly shown during “edit” mode.
BUT the values are not saved, and if there was a PRIOR value it is no longer displayed in view mode.
-
I’ve tried several functions, they all have the same problem.
For example I enter “getCities” in the Choices Callback field, and “No Selected” for the Parent Option, and “View everywhere” for Visibility
And I use this function:
function getCities() { $cities = [ "Paris" =>"Paris", "Marseille" => "Marseille", "Lyon" => "Lyon" ]; return $cities; }
Hi,
If you are testing this with UM build it country field, this won’t work since UM country field returns the country code not the exact name. Following function will work with our build-in country field
function getStates() { //get the value from the 'parent' field, sent via the AJAX post. $choice = $_POST['parent_option']; //Depending on the value of $choice, return a different array. switch($choice) { case "FR": $states = array( "Paris" =>"Paris", "Marseille" => "Marseille", "Lyon" => "Lyon" ); break; case "ES": $states = array( "Madrid"=>"Madrid", "Barcelona"=>"Barcelona" ); break; default: //code to do something if other options are not selected (throw an error, or set $cities to a default array) $states = array("no state"); } return $states; }
Thanks.
I am not using country code. And I am not using Parent Option, it is not selected.
As I said earlier, I’ve tried several functions, they all have the same problem.
Please try any simple function and test. For example:
function SelectColor() { $colors = [ "Red" =>"Red", "Green" => "Green", "Blue" => "Blue" ]; return $colors; }
@mansurahamed, have you tested the Choices Callback field with any function to verify it works in V2.0.44 thru 2.0.48 ??
If so can you provide a link to your test site to show it working?
- This reply was modified 5 years, 6 months ago by artmerk.
Hi @artmerk,
function SelectColor() { $colors = [ "Red" =>"Red", "Green" => "Green", "Blue" => "Blue" ]; return $colors; }
Your code is working fine. I can save value. I have tested in local server. You can use https://www.apachefriends.org/ to check.
Thanks.
I obviously cannot access your local server to see your test.
I have created a WordPress installation where you can login and see the problem.
Login here:
https://outstanding-vole.w6.wpsandbox.pro/wp-login.php
username: admin
password: adminpassTo see the plugin installed and the profile form created:
https://outstanding-vole.w6.wpsandbox.pro/wp-admin/Then can test a normal dropdown and a callback dropdown here and see the error.
Here is the user profile:
https://outstanding-vole.w6.wpsandbox.pro/user/admin/And here is where you can edit and see the callback options:
https://outstanding-vole.w6.wpsandbox.pro/user/admin/?profiletab=main&um_action=editAnd then back to the user profile where you will see the callback field value is NOT saved:
https://outstanding-vole.w6.wpsandbox.pro/user/admin/Hi @artmerk,
I just added the same code in your theme’s functions.php file and renamed the function to NewSelectColor . Works like a charm. Don’t know why it didn’t work for your. If you can see the dropdown values at the edit field screen it will work perfectly.
Thanks.
Thanks so much for your response. I see you have the “test” function working for colors.
Now, please look at the test site again, where I have included the actual function used in my site (also added to the theme’s functions.php file)
function get_username_hrefs() { // sort by first name $users = get_users(array( 'meta_key' => 'first_name', 'orderby' => 'meta_value', )); foreach ( $users as $user ) { $user_names[$user->id] = "<a href='/user/" . $user->user_login . "/'>" . $user->display_name . "</a>"; } return $user_names; }
I can see the dropdown values at the edit screen. BUT the values are not saved. Please help?
Note: this exact function does work in V2.043 or earlier. thank you.
Hi @artmerk,
I have changed it to
function get_username_hrefs() { // sort by first name $users = get_users(array( 'meta_key' => 'first_name', 'orderby' => 'meta_value', )); foreach ( $users as $user ) { $user_names[$user->display_name] = $user->display_name; } return $user_names; }
and created a new dropdown for it, showing perfectly. You can’t use html code inside dropdown.
Thanks.
Thank you for your help.
But my original function with html code DOES work in V2.0.43
So why does it NOT WORKING in V2.0.44 and higher?
Choices Callback is very powerful, but now you cannot use a simple concatenation (NO html) like:
$user_names[$user->display_name] = $user->display_name . “asdf”;Now my functions are no longer compatible with the UM current version. If I upgrade, I will lose all the data in those user meta fields! This is an important feature that is no longer available in current version?
Help please?
- The topic ‘Choices Callback – NOT WORKING in V2.0.44 and higher’ is closed to new replies.