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 function 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 your theme's 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.
	$countries = array(
		"FR" => "France",
		"ES" => "Spain",
		"IT" => "Italy",
	);

	return array_unique( $countries );
}

Image - The custom  Country field settings.

Example 2 - Make a specific dropdown field that displays different array of options depending on the parent field

Add the custom function custom_city_choices_callback to your theme's 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) & 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( $has_parent = false ) {

	// 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_options = isset( $_POST['parent_option'] ) ? $_POST['parent_option'] : array();
	if ( ! is_array( $parent_options ) ) {
		$parent_options = array( $parent_options );
	}

	// get related options for the parent field choice.
	$arr_options = array();
	if ( empty( $parent_options ) ) {
		foreach ( $all_options as $k => $opts ) {
			$arr_options = array_merge( $opts, $arr_options );
		}
	} else {
		foreach ( $parent_options as $parent_option ) {
			if ( isset( $all_options[$parent_option] ) ) {
				$arr_options = array_merge( $arr_options, $all_options[$parent_option] );
			}
		}
	}

	// Optional. Display this if there are no related options for this choice.
	if ( empty( $arr_options ) ) {
		$arr_options[] = 'No cities found';
	}

	return array_unique( $arr_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.

For customization of your custom fields callback cropdowns, read this article: Custom Callback Dropdowns