Created
October 18, 2023 10:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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