Skip Navigation

[Resolved] Select field get options from shortcode not working

This support ticket is created 2 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 8.55 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by chrisB-36 2 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2279975

Tell us what you are trying to do?

I'm trying to use the get options from shortcode feature with a generic select field to fill the select options with values from one of my custom post types. I have validated that my shortcode is returning valid JSON yet the select field on the form is empty.

Is there any documentation that you are following?

I have reviewed similar tickets on this topic but have not found one that matches my situation. I am not using a View, simply my own PHP code in the shortcode.

Is there a similar example that we can see?

Here is the code for the field on the form:

<label for="%%FORM_ID%%_five-faves">[cred_i18n name='five-faves-label']My Faves[/cred_i18n]</label>
[cred_generic_field type='select' field='five-faves']
{
"required":0,
"options":[ [bman-fave-courses-select-shortcode] ]
}
[/cred_generic_field]
</div>

And here is the code from my custom shortcode:

$courselist = new WP_Query( $args );
while ( $courselist->have_posts() ){
$courselist->the_post();
$data[] = array('value' => get_post_field( 'post_name', get_the_ID() ), 'label' => get_the_title() );
}
wp_reset_postdata();

// save the output in the database for debugging purposes
update_post_meta(14867, "bmantest", json_encode($data));

// return the them in json format for the generic field
return(json_encode($data));

Notice that I save the returned output in the database to help with debugging. I have validated that output as valid JSON. Here's a sample of it (too long to paste the entire output here) showing the first two entries and the last two entries:

[{"value":"birkdale-golf-club","label":"Birkdale Golf Club"},{"value":"carolina-lakes-golf-club","label":"Carolina Lakes Golf Club"},

...

{"value":"warrior-golf-club","label":"Warrior Golf Club"},{"value":"waterford-golf-club","label":"Waterford Golf Club"}]

And finally, here is the HTML output that is generated on the front end when I test the form:

<label for="cred_user_form_14872_1_1_five-faves">My Faves</label>
<div class="js-wpt-field-items js-wpt-repetitive wpt-repetitive" data-initial-conditional="" data-item_name="select-five-faves">
<select id="cred_user_form_14872_1_1_five-faves" class=" wpt-form-select form-select select" preset_value="" cred_generic="1" placeholder="" preview="" previewsize="" urlparam="" data-wpt-type="select" name="five-faves">
<option value="" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_user_form_14872_1_1_cred_user_form_14872_1_1_five-faves" data-wpt-name="five-faves" selected="selected">--- not set ---</option>
</select>

What is the link to your site?

#2280379

Hi,

Thank you for contacting us and I'd be happy to assist.

Looking into the code you've shared, I suspect that your generic field is not generating properly due to an extra pair of square brackets ([ ]).

Can you please make the following two changes:

1. Change the options line in the generic field code to:


"options":[bman-fave-courses-select-shortcode]

2. In the line that returns the custom shortcode's output, you can trim out the square brackets:


return trim(json_encode($data), '[]');

I hope this helps and let me know how it goes.

regards,
Waqar

#2280415

Suggestion #1: I think you may have misunderstood the code I shared. That was the code in the expert view with the double brackets. With expert mode off, I had my shortcode enclosed in the single brackets already, just as you suggested. So nothing to change for this suggestion.

#2 did the trick, however!

My issue is resolved now. Thank you!