Skip Navigation

[Resolved] Add select option by shortcode

This support ticket is created 7 years 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by Beda 7 years ago.

Assisted by: Beda.

Author
Posts
#582953

I am trying to:
I am trying to add options by shortcode in generic multi select box. I have added this shortcode in functions.php and called it in select box options. Here is the code

function get_parents($atts) {
global $current_user;
get_currentuserinfo();
$author_query = array('post_type' => 'certification', 'posts_per_page' => '-1','author' => $current_user->ID,);
$author_posts = new WP_Query($author_query);
$parent_ids = "";
while($author_posts->have_posts()) : $author_posts->the_post();
$options[] = array(
'value' => get_the_ID(),
'label' => get_the_title(),
);
endwhile;
$options = json_encode($options);
return $options;
}
add_shortcode('get-parents', 'get_parents');

[cred_generic_field field='related_certifications' type='multiselect' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ get-parents ]
}
[/cred_generic_field]

#582959
Create Generic field with Values in CRED.png

This falls under Custom Code, as Toolset does not provide a method to generate valid JSON Code to populate a CRED generic Field with it.

I can however shortly assist the issue:

1. You can insert CRED generic Fields and either populate them directly with values:

[cred_generic_field field='multi_select' type='multiselect' class='' urlparam='']
  {
  "required":0,
  "validate_format":0,
  "default":[],
  "options":[
    {"value":"value1","label":"label1"},
    {"value":"value2","label":"label2"},
    {"value":"value3","label":"label3"}
  ]
  }
[/cred_generic_field]

You also have the option to use a ShortCode to generate the field options on the fly. Any shortcode which will generate a valid JSON format of options can be used.

Your ShortCode needs to return something that mimics this exact Syntax:

{"value":"value1","label":"label1"},
{"value":"value2","label":"label2"},
{"value":"value3","label":"label3"}

Notice that the last value should not be followed by a comma.
Note also that any deviation from that syntax will break the field.

Please var_dump() the value that $options holds in your code.
It should match the exact syntax above, then it will work.

Please let me know if you need more information!

#582986

Thanks for the response Beda. My function is also generating json response like you suggested. Here is the function again :

function get_parents($atts) {
global $current_user;
get_currentuserinfo();
$author_query = array('post_type' => 'certification', 'posts_per_page' => '-1','author' => $current_user->ID,);
$author_posts = new WP_Query($author_query);
$parent_ids = "";
while($author_posts->have_posts()) : $author_posts->the_post();
$pid = get_the_ID();
$ptitle = get_the_title();
$options[] = array(
"value" => "$pid",
"label" => "$ptitle",
);
endwhile;
$options = json_encode($options);
return $options;
}

Can you please look into this and help me.

#583182

Hi Beda,

Please let me know if you need any other details to debug the issue.

#583858

We cannot debug or provide Custom Code.
https://toolset.com/toolset-support-policy/

What I can do is help you to debug your code, for that I asked the var_dump() informations of that code:
https://toolset.com/forums/topic/add-select-option-by-shortcode/#post-582959

Then you will know what is returned, and can adjust adequately.
There is n o reason for the fields to break if the output is correct - I tested an output locally using a Custom ShortCode that returns the exactly needed JSON syntax data.

What does var_dump() of $options produce?