Populating Dropdown Menus For Ultimate Members Plugin Using the Choices Callback with an External API

I’m trying to create dynamic dropdown fields using the Ultimate Members plugin. I’ve managed to use the US Census api to create two drop down menus for a registration form that populates a states menu, and a county menu that gets the counties available for each state. Everything works fine in the registration form, the problem is that when the user submits the registration form the county name that is sent to the admin email for account approval is empty(the state value is being sent correctly.

I would appreciate it if somebody could point out where I went wrong. I’m using the choices callback feature and have added the following code to my themes functions.php file:

function custom_state_choices_callback() {
// Fetch states from U.S. Census Bureau API
$states_url = 'https://api.census.gov/data/2019/pep/population?get=NAME...😘';
$response = wp_remote_get($states_url);
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Initialize an empty array for states
$states = array();
// Populate the states array
if (!empty($data) && is_array($data)) {
foreach ($data as $index => $state) {
if ($index === 0) continue; // Skip the header row
$state_code = $state[1];
$state_name = $state[0];
$states[$state_code] = $state_name;
}
}
return array_unique($states);
}

// Function to populate the county dropdown based on the selected state function
custom_county_choices_callback($has_parent = false) {
// Initialize an empty array for counties
$counties = array();
// Get the selected state from the parent field
if ($has_parent) {
// Get the selected state from the parent field
$parent_options = isset($_POST['parent_option']) ? $_POST['parent_option'] : array();
if (!is_array($parent_options)) {
$parent_options = array($parent_options);
}
// Fetch counties for each selected state
foreach ($parent_options as $state_code) {
$counties_url = "https://api.census.gov/data/2019/acs/acs5?get=NAME&for=county:*&in=state:$state_code";
$response = wp_remote_get($counties_url);
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (!empty($data) && is_array($data)) {
foreach ($data as $index => $county) {
if ($index === 0) continue; // Skip the header row
$county_name = explode(',', $county[0])[0];
$counties[$county_name] = $county_name;
}
}
}
}
// Optional: Display this if there are no related options for this choice
if (empty($counties)) {
$counties[''] = 'No counties found';
}
return array_unique($counties);
}

// Hook the functions to Ultimate Member's Choices Callback
add_filter('um_custom_field_choices_callback', function($choices, $field) {
if ($field['meta_key'] === 'service_state') {
$choices = custom_state_choices_callback();
} elseif ($field['meta_key'] === 'service_county') {
$choices = custom_county_choices_callback(true);
}
return $choices;
}, 10, 2);````



TIA

I initially tried creating a custom plugin and using JavaScript to populate the dropdown menus. Everything works in the registration form, but the state and county values were empty in the email that's sent to the admin.

I tried another approach and used the Callback Choices feature and made it one step further with the state value being sent to the admin email, but the county value that's sent to the admin is still empty.

New contributor

Lindy Ramirez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật