Choices API

Many settings in Make need to be limited to a value that comes from a specific set of values, rather than having an arbitrary value. These possible values are called “choices” in Make, and they are managed via the Choices API.

A choice set consists of a set ID that corresponds to an array of value/label pairs. These arrays can be used directly in the choices argument for a Select or Radio control in the WordPress Customizer. Here’s an example choice set:

array(
	'post-author' => array(
		'avatar' => __( 'With avatar', 'make' ),
		'name'   => __( 'Without avatar', 'make' ),
		'none'   => __( 'None', 'make' ),
	),
)

The make_update_choice_set() function makes it easy to add a new choice set or update an existing one.


Example: Add a choice set and update an existing one

The following could be used in a child theme’s functions.php file or in a plugin:

function childtheme_update_choice_sets() {
	// Add a new set
	make_update_choice_set(
		'rice-types',
		array(
			'white' => __( 'White rice', 'make' ),
			'brown' => __( 'Brown rice', 'make' ),
			'wild'  => __( 'Wild rice', 'make' ),
			'black' => __( 'Forbidden rice', 'make' ),
		)
	);

	// Update an existing set
	make_update_choice_set(
		'post-author',
		array(
			'avatar' => __( 'With avatar', 'make' ),
			'name'   => __( 'Without avatar', 'make' ),
			'none'   => __( 'None', 'make' ),
			'wapuu'  => __( 'With random Wapuu', 'make' )
		)
	);
}

add_action( 'make_choices_loaded', 'childtheme_update_choice_sets' );

In the above example, the make_update_choice_set() function takes two parameters: a unique string for the choice set ID, and an associative array of choices.