Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Created December 6, 2024 21:09
Show Gist options
  • Save yuriinalivaiko/0a804d322f7ff19fc2594a1e029ae8e7 to your computer and use it in GitHub Desktop.
Save yuriinalivaiko/0a804d322f7ff19fc2594a1e029ae8e7 to your computer and use it in GitHub Desktop.
Example. Choices Callback Feature. 4 related dropdowns.
<?php
// Planet field.
function custom_planet_choices_callback() {
// choices array.
$options = array(
"earth" => "Earth",
"moon" => "Moon",
"mars" => "Mars",
);
return array_unique( $options );
}
// Continent field.
function custom_continent_choices_callback() {
// choices array.
$all_options = array(
"earth" => array(
"africa" => "Africa",
"america" => "America",
"eurasia" => "Eurasia",
),
"moon" => array(
"moon_t" => "Terrans",
"moon_p" => "Protoss",
"moon_z" => "Zerg",
),
"mars" => array(
"mars_t" => "Terrans",
"mars_p" => "Protoss",
"mars_z" => "Zerg",
),
);
$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 );
}
// Country field.
function custom_country_choices_callback() {
// choices array.
$all_options = array(
"africa" => array(
"EG" => "Egypt",
"KE" => "Kenya",
),
"america" => array(
"CA" => "Canada",
"US" => "United States",
),
"eurasia" => array(
"FR" => "France",
"ES" => "Spain",
),
"moon_t" => array(
"mot1" => "Terrans 1",
"mot2" => "Terrans 2",
),
"moon_p" => array(
"mop1" => "Protoss 1",
"mop2" => "Protoss 2",
),
"moon_z" => array(
"moz1" => "Zerg 1",
"moz2" => "Zerg 2",
),
"mars_t" => array(
"mat1" => "Terrans 1",
"mat2" => "Terrans 2",
),
"mars_p" => array(
"map1" => "Protoss 1",
"map2" => "Protoss 2",
),
"mars_z" => array(
"maz1" => "Zerg 1",
"maz2" => "Zerg 2",
),
);
$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 );
}
// City field.
function custom_city_choices_callback() {
// choices array.
$all_options = array(
"EG" => array(
"Alexandria" => "Alexandria",
"Cairo" => "Cairo",
),
"KE" => array(
"Nairobi" => "Nairobi",
"Mombasa" => "Mombasa",
),
"CA" => array(
"Toronto" => "Toronto",
"Montreal" => "Montreal",
),
"US" => array(
"New York City" => "New York City",
"Los Angeles" => "Los Angeles",
),
"FR" => array(
"Paris" => "Paris",
"Marseille" => "Marseille",
"Lyon" => "Lyon",
),
"ES" => array(
"Madrid" => "Madrid",
"Barcelona" => "Barcelona",
),
// sorry, I'm too lazy to add cities for Moon and Mars.
"mot1" => array(),
"mot2" => array(),
"mop1" => array(),
"mop2" => array(),
"moz1" => array(),
"moz2" => array(),
"mat1" => array(),
"mat2" => array(),
"map1" => array(),
"map2" => array(),
"maz1" => array(),
"maz2" => array(),
);
$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 );
}
@yuriinalivaiko
Copy link
Author

This gist is an example for the article Choices Callback Feature in UM 2.1+

Screenshots

Planet field.
planet

Continent field.
continent

Country field.
country

City field.
city

Expected result in the form

example

@unbroken-untamed
Copy link

I want to generate a Unique User ID from the details that users enter for State, Local Government, Ward, and their WordPress assigned ID.

Each Local Government has Local Government Name and Local Government Code.

Each Ward has Ward Name and Ward code.

For example, Abia State has Abia North as a Local Government, and the Local Government code is 01.

Abia North Local Government has many Wards, and two Wards are listed below with their Ward codes:

Eziama with Code 01.
Osusu with Code 02.

THE PROCESS
Use the Choices callback to auto-populate a Text field with the correct Local Government Code when a Local Government Name is chosen.

And also use the Choices callback to auto-populate a Text field with the correct Ward Code when a Ward Name is chosen.

On the Registration Form -- State, Local Government Name, and Ward name will be available and visible to the user.

But Local Government Code and Ward code will not be visible to the user on the Registration Form.

However, if someone selects Abia North as his/her Local Government, a Text field called "Local Government Code" will be auto-populated with 01.

And if they select Osusu as their Ward— a Text field called "Ward code" will be auto-populated with 02.

Then the system will add their WordPress assigned ID to generate the Unique User ID on Form Submission.

THE RESULT
The User ID will be: 01-02-18, gotten from:

Local Government: Abia North with code 01.
Ward: Osusu with code 02.
WordPress ID: 18

QUESTION
Please, how do I achieve this?

Special Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment