Skip Navigation

[Resolved] Select JSON options not outputting correctly

This thread is resolved. Here is a description of the problem and solution.

Problem:
Dropdown select created using JSON view not outputting correctly and how to output clean raw output with view to build JSON view

Solution:
You can use view's filter wpv_filter_wpv_view_shortcode_output in order to set view to output raw clean output.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/select-json-options-not-outputting-correctly/#post-902704

Relevant Documentation:

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

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by jamesT-5 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#902575

I am trying to: allow a user to select a post author when submitting a cred form.

Link to a page where the issue can be seen: hidden link (choose guest at the top)

I expected to see: a dropdown with current users.

Instead, I got: blank because the view is not outputting correctly.

Hello,

I've folllowed this guide exactly: https://toolset.com/forums/topic/cred-for-to-select-post-author/

The view is outputting the following: {"value":"19","label":"createteam"} and i have put the view in the page as normal at the bottom.

When I paste the output into the options brackets in the CRED form, it does not work.

#902704

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I removed the following code from your functions.php file as its not needed:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data){
// if a specific form
if ($form_data['id']==514){
$my_post = array(
      'ID'           => $post_id,
      'post_author' => $form_data['']
  );
  
// Update the post into the database
  wp_update_post( $my_post );
add_post_meta($post_id, '__my_custom_field', $_POST['my_autor_select'], true);
   
}
}

I've added following view'ss hook to your functions.php file that is used to create raw output for views. please note that raw output views does not support paginations etc..etc as you are set this view to output raw text content.


add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 function prefix_clean_view_output( $out, $id ) {
if ( $id == '522' ) { //Change this to your View ID
$start = strpos( $out, '<!-- wpv-loop-start -->' );
if ( 
$start !== false
&& strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
) {
$start = $start + strlen( '<!-- wpv-loop-start -->' );
$out = substr( $out , $start );
$end = strrpos( $out, '<!-- wpv-loop-end -->' );
$out = substr( $out, 0, $end );
}
}
return $out;
}

Now, with your CRED form I've change the generic field name to some meaningful name as: wpv_author_id

[cred_generic_field field="wpv_author_id" type="select" class="" urlparam=""]{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[[wpv-view name='author-list']]
}
[/cred_generic_field]

Now, whenever you will save the form, it will save the author ID with post meta key = wpv_author_id

If you will check the URL - I can see the dropdown working fine:
=> hidden link

Could you please confirm you are able to see the dropdown as well.

#902790

Thanks Minesh! I have just had a look and the drop down is now populating, but only with a single user result. I've looked through the query and it should be outputting all of the users - there's no further filter as it's set to "All Roles".

The workaround to get this functionality working is quite complex when Types is designed to avoid writing PHP, is there a plan to make this more user-friendly in future or perhaps a parameter for the views shortcode raw=true perhaps?

Thanks,

James

#902794

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I changed your view's loop as given under:

<wpv-loop>[wpv-item index=1]{"value":"[wpv-user field="ID"]","label":"[wpv-user field="nickname"]"}[wpv-item index=other],{"value":"[wpv-user field="ID"]","label":"[wpv-user field="nickname"]"}</wpv-loop>

When I visit the link you shared - I can see all users now - could you please check and confirm it works at your end as well:
hidden link

more user-friendly in future or perhaps a parameter for the views shortcode raw=true perhaps:
==> We already filed a feature request for this and you will see this will be implemented in near future.

#902878

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please confirm - now you can see all users with dropdown.

#902880

Thanks very much for your help, I can see it now!