Choices Callback Feature in UM 2.1+
Overview
This document provides information on how to set up the Choices Callback feature to make a specific dropdown field and the Parent Option feature to display different array of options depending on the user choice in the parent field.
This article is actual for the Ultimate Member 2.1 and next versions. See this article for older versions.
Custom function
The Choices Callback value is a name of your custom function that returns an array of options for the field. Array keys are used as option names and array values are used as option titles. You can add your custom functions to the functions.php file in the theme directory.
Examples
Example 1 - Make a specific dropdown field
Add the custom function custom_country_choices_callback
to the functions.php file. Add a custom dropdown field Country to your form and paste the function name "custom_country_choices_callback" to the field's Choices Callback setting.
function custom_country_choices_callback() { // choices array. $options = array( "FR" => "France", "ES" => "Spain", "IT" => "Italy", ); return array_unique( $options ); }
Image - The custom Country field settings.
Example 2 - Make a specific dropdown field that displays different options depending on the parent field
Add the custom function custom_city_choices_callback
to the functions.php file. Add a custom dropdown field City to your form and paste the function name "custom_city_choices_callback" to the field's Choices Callback setting. Select the Country field in the Parent Option setting to link these fields.
Here is a function that you can use to get cities for France (FR) and Spain (ES). If you select any of these countries in the Country field, cities will be show up as options in the City field.
function custom_city_choices_callback() { // choices array. $all_options = array( "FR" => array( "Paris" => "Paris", "Marseille" => "Marseille", "Lyon" => "Lyon" ), "ES" => array( "Madrid" => "Madrid", "Barcelona" => "Barcelona" ), ); // get values from the parent field, sent via the AJAX post. $parent = empty( $_POST['parent_option'] ) ? '' : sanitize_text_field( $_POST['parent_option'] ); $options = ( $parent && array_key_exists( $parent, $all_options ) ) ? $all_options[ $parent ] : array(); return array_unique( $options ); }
Image - The custom City field settings.
Image - Two specific dropdown fields where the City field options depend on the choice in the Country field.
Example 3 - Four related dropdown fields linked using the Parent Option setting
This example is too big for an article, see the code and screenshots on GitHubGist.