Skip Navigation

[Resolved] Error using cred_generic_field to let user select the post author

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

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 3 replies, has 3 voices.

Last updated by Matthew 8 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#346232

I built a CRED form where the user can select the author of the post too.
I Built it following the advices found here: https://toolset.com/forums/topic/cred-for-to-select-post-author/
The form was running very well since some days ago (unfortunately I can't be more specific, since just today we find it is no more running right ) but now it doesn't output anymore the select dropdown with the users.

So I have the view that list all the authors. It's named aux_list_author :

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start -->
	<wpv-loop>[wpv-item index=1]{"value":"[wpv-user field="ID"]","label":"[wpv-user field="user_login"]"}[wpv-item index=other],{"value":"[wpv-user field="ID"]","label":"[wpv-user field="user_login"]"}</wpv-loop>
	<!-- wpv-loop-end -->[/wpv-items-found][wpv-layout-end]

And the CRED form with the cred_generic_field in it

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

As I said all was fine until some days ago, maybe a week or little more, but now the whole cred_generic_field doesn't output anything.

If I put the wpv-view shortcode on it's own in the CRED form (i.e. not inside the cred_generic_field ) I can see it's output, and apparentely it all right:

{"value":"10","label":"AduaWorkSpace"},{"value":"28","label":"almabc"},{"value":"24","label":"ariesep"},{"value":"19","label":"Businessinmotion"},{"value":"29","label":"Cheren"},{"value":"18","label":"cogesta"},{"value":"25","label":"darsena"},{"value":"16","label":"dimora"},{"value":"27","label":"ermelinda"},{"value":"22","label":"europa-coworking"},{"value":"32","label":"eurtc"},{"value":"33","label":"executive_monti"},{"value":"21","label":"getanoffice"},{"value":"20","label":"marconiwo"},{"value":"8","label":"mbcenter"},{"value":"34","label":"neaservice"},{"value":"13","label":"omicron"},{"value":"9","label":"parcodemedici"},{"value":"31","label":"polaris"},{"value":"12","label":"salario-office"},{"value":"35","label":"segreterie-virtuali"},{"value":"17","label":"simal"},{"value":"26","label":"tiempona"},{"value":"11","label":"TiempoRoma"},{"value":"15","label":"trade-ge"},{"value":"23","label":"veryoffice"},{"value":"14","label":"worldservice-mi"}

I investigated some more and took a look at the html source of the CRED form, and I found that the output of [wpv-view name="aux_list_author"] is surronded by and aditional div that contains an input too:

<div id="wpv-view-layout-1229-CPID53" class="js-wpv-view-layout js-wpv-layout-responsive" data-viewnumber="1229-CPID53" data-pagination="{"ajax":"false","effect":"fade","duration":500,"stop_rollover":"false","cache_pages":true,"preload_pages":true,"pre_reach":1,"spinner":"default","spinner_image":"<em><u>hidden link</u></em>","callback_next":"","max_pages":1,"page":1}">
<input type="hidden" id="js-wpv-pagination-page-permalink" value="/inserisci-sede/?wpv_view_count=1229-CPID53&wpv_paged=1" />
	{"value":"10","label":"AduaWorkSpace"},{"value":"28","label":"almabc"},{"value":"24","label":"ariesep"},{"value":"19","label":"Businessinmotion"},{"value":"29","label":"Cheren"},{"value":"18","label":"cogesta"},{"value":"25","label":"darsena"},{"value":"16","label":"dimora"},{"value":"27","label":"ermelinda"},{"value":"22","label":"europa-coworking"},{"value":"32","label":"eurtc"},{"value":"33","label":"executive_monti"},{"value":"21","label":"getanoffice"},{"value":"20","label":"marconiwo"},{"value":"8","label":"mbcenter"},{"value":"34","label":"neaservice"},{"value":"13","label":"omicron"},{"value":"9","label":"parcodemedici"},{"value":"31","label":"polaris"},{"value":"12","label":"salario-office"},{"value":"35","label":"segreterie-virtuali"},{"value":"17","label":"simal"},{"value":"26","label":"tiempona"},{"value":"11","label":"TiempoRoma"},{"value":"15","label":"trade-ge"},{"value":"23","label":"veryoffice"},{"value":"14","label":"worldservice-mi"}
</div>

So I think that maybe the cred_generic_field "crashes" when it finds inside it's options the div and the input and not only the value:label items, isn't it?

Could it be a bug introduced in a recent Views' update? or am I doing something wrong?

Could you please help me?

Stefano

#346275

Thank you for contacting us here in the Support Forum and for providing the Debug Informations

Using Views as a Data Provider is, and never was the directly intended goal of the Plugin.

It's intended as a display engine and since the latest update, default HTML is outputted with the Loop to ensure full functionality of the render engine in case of Endless pagination (scroll) etc.

This default HTML breaks the JSON you generated with the View.

You can avoid this with a Custom Filter.
Insert this to your functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
    if ( $id == '375' ) {
        $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;
}

Replace the Views ID with the ID of the View generating your JSON.

Please acknowledge, that this is a code that will not allow any other features in your View but data output.
Pagination, AJAX, parametric searches etc will be broken.

Please let me know if you need further infos about this.
and let me know if the above solution works for you, I look forward to your reply!

Thank you for your patience.

#347121

Thank you for your answer.

Since I understood that using Views in that way is not recommended, and since you suggested to write some code anyway I ended up writing a new custom shortcode:

/*  Json User List shortcode
 *  usage: [json-users-list role="{otpional role; default all}" label="{optional. The field you want displayed as label in the dropdown}" orderby="optional. The filed to order by the list"]
 */
 function wpmania_json_users_list( $atts ) {

	// Attributes
	extract( shortcode_atts(
		array(
			'role' => '',
			'label' => 'display_name',
			'orderby' => 'display_name'
		), $atts )
	);
	$args = array(	'role'=>$role, 
					'fields' => array('ID',$label),
					'orderby' => $orderby);
	$users = get_users( $args );
	$json = json_encode($users);
	$json = str_replace('"ID"','"value"',$json);
	$json = str_replace('"display_name"','"label"',$json);
	return $json;
}
add_shortcode( 'json-users-list', 'wpmania_json_users_list' );

Thank you

#526731

Hey Beda, seems that using the trick to clean the view of the wrapping div doesn't work if the view is embedded within the theme itself, any ideas on that scenario?

These wrappers often break what I am up to by fouling up the css. Still love all your plugins! Thanks!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.