In a CRED form, I am trying to dynamically generate the list of options that should go into a "Select" generic field like this:
[credform]
[cred_field field='form_messages' class='alert alert-warning']
<div class="form-group">Select a new author:</div>
[cred_generic_field field="my_author_select" type="select" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":"",
"options":[list-of-authors-options]
}
[/cred_generic_field]
[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]
The shortcode [list-of-authors-options] is in my function.php and is as follows:
// Add Shortcode to Get a List of authors
add_shortcode("list-of-authors-options", "list_of_authors_options");
function list_of_authors_options() {
$out = '';
$users_subscriber = get_users(array( 'role' => 'Subscriber' ));
$users_superuser = get_users(array( 'role' => 'Superuser' ));
$users = array_merge($users_subscriber,$users_superuser);
$count = count($users);
$i=1;
foreach ($users as $user) {
if ($i==$count) {
$out .= '{"value";"' . $user->ID . '","label":"' . $user->display_name . '"}';
}else{
$out .= '{"value";"' . $user->ID . '","label":"' . $user->display_name . '"},';
}
$i =$i + 1;
}
return $out;
}
If I insert this shortcode on a page, I can clearly see the output as follows which is the proper required format. However, the CRED form shows no options whatsoever.
{"value";"1","label":"name1"},{"value";"2","label":"name2"},{"value";"3","label":"name3"},{"value";"4","label":"name4"}
I also tried to implement an alternate solution by using a View named "authors-dropdown-options" as a shortcode argument for the "options" parameter like this:
"options":[[wpv-view name="authors-dropdown-options"]]
Here is the Views code:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=other]
{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"},
[wpv-item index=last]
{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-layout-end]
Again inserting this view on a page does generate the correct output, but it does not work in the CRED form.
Please help. Thank you.