Hi there,
I'm creating a user CRED form and have added a generic "select" (pull-down menu) field. I'm attempting to populate the contents of the select field from a shortcode, but it's not working. The pull-down menu appears on the webpage, but none of the options that are supposed to be there are visible. I'm attaching some screenshots to show the field settings and the field as it appears on the frontend.
Here is the code for my shortcode, too:
function cap_print_countries ( $atts, $content = "" ) {
$countries = [
'USA',
'Canada',
'Mexico',
] ;
$json_countries = [] ;
foreach ( $countries as $country ) {
// Per Toolset, here's the proper format:
// {"value": "value1", "label": "Label 1"}, {"value": "value2", "label": "Label 2", "default": true}
$json_countries[] = [ 'value' => $country, 'label' => $country] ;
}
return wp_json_encode ($json_countries) ;
}
add_shortcode ('cap_print_countries','cap_print_countries') ;
Can someone tell me what I'm doing wrong? Thank you!
Saul
For anyone else stumped by this, wp_json_encode() was outputting the following:
[{"value":"USA","label":"USA"},{"value":"Canada","label":"Canada"},{"value":"Mexico","label":"Mexico"}]
The beginning and ending square brackets were the culprit. I removed them as follows:
return trim (wp_json_encode ($json_countries),'[]') ;