Skip to content

Instantly share code, notes, and snippets.

@nikitasinelnikov
Created November 27, 2024 11:46
Show Gist options
  • Save nikitasinelnikov/7f7cc12ac38ea211b30868c509d7cae1 to your computer and use it in GitHub Desktop.
Save nikitasinelnikov/7f7cc12ac38ea211b30868c509d7cae1 to your computer and use it in GitHub Desktop.
Ultimate Member: new UI and callback functions
function um_custom_country_choices_callback() {
// choices array.
$countries = array(
"FR" => "France",
"ES" => "Spain",
"IT" => "Italy",
);
return array_unique( $countries );
}
if ( UM()->is_new_ui() ) {
function um_custom_city_choices_callback( $parent_options = array(), $parent_key = null ) {
// choices array.
$all_options = array(
"FR" => array(
"Paris" => "Paris",
"Marseille" => "Marseille",
"Lyon" => "Lyon"
),
"ES" => array(
"Madrid" => "Madrid",
"Barcelona" => "Barcelona"
),
);
// get related options for the parent field choice.
$arr_options = array();
if ( empty( $parent_options ) ) {
// Show all child options for empty parent. It's for member directory filter.
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] );
}
}
}
return array_unique( $arr_options );
}
} else {
function um_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 (dynamic change parent dropdown on a page).
$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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment