Skip Navigation

[Resolved] Question about populating a generic field via shortcode on a CRED form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by Saul Baizman 1 year, 9 months ago.

Author
Posts
#2560987
field as it appears on the frontend.png
field settings.png

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

#2561005

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),'[]') ;