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]
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!
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.
Hi Beda,
Please let me know if you need any other details to debug the issue.
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?