Skip to content

Instantly share code, notes, and snippets.

@nikitasinelnikov
Created October 18, 2023 10:37
Show Gist options
  • Save nikitasinelnikov/95cdb09585299764795de7231007bcba to your computer and use it in GitHub Desktop.
Save nikitasinelnikov/95cdb09585299764795de7231007bcba to your computer and use it in GitHub Desktop.
Ultimate Member + Country State City Dropdown PRO: Custom callbacks for select fields Country, State, City
function um_customcb_populate_countries() {
$countries = array();
if ( ! function_exists( 'get_countries' ) ) {
return $countries;
}
$all_countries = get_countries();
if ( empty( $all_countries ) ) {
return $countries;
}
foreach ( $all_countries as $country ) {
$countries[ $country->id ] = $country->name;
}
return $countries;
}
function um_customcb_populate_states() {
$states = array();
if ( empty( $_POST['parent_option'] ) ) {
return $states;
}
if ( ! function_exists( 'get_states_by_country_id' ) ) {
return $states;
}
if ( is_array( $_POST['parent_option'] ) ) {
$choices = array_map( 'absint', $_POST['parent_option'] );
} else {
$choices = array( absint( $_POST['parent_option'] ) );
}
foreach ( $choices as $choice ) {
$all_states = get_states_by_country_id( $choice );
if ( empty( $all_states ) ) {
continue;
}
foreach ( $all_states as $state ) {
$states[ $state->id ] = $state->name;
}
}
return $states;
}
function um_customcb_populate_city() {
$cities = array();
if ( empty( $_POST['parent_option'] ) ) {
return $cities;
}
if ( ! function_exists( 'get_cities_by_state_id' ) ) {
return $cities;
}
if ( is_array( $_POST['parent_option'] ) ) {
$choices = array_map( 'absint', $_POST['parent_option'] );
} else {
$choices = array( absint( $_POST['parent_option'] ) );
}
foreach ( $choices as $choice ) {
$all_cities = get_cities_by_state_id( $choice );
if ( empty( $all_cities ) ) {
continue;
}
foreach ( $all_cities as $city ) {
$cities[ $city->id ] = $city->name;
}
}
return $cities;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment