Skip Navigation

[Resolved] Select page template in CRED form? (Part2)

This support ticket is created 2 years, 8 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by a.R 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2314857

a.R

I was told, one can use the field "_views_template" to store the TOOLSET-Template for a post.
But

[cred_field field="_views_template" output="bootstrap"]

or anything similar just gives me an error.
How would that work?

Would it be able to let users choose between some templates by radio or so?

#2315981

Hi,

Thank you for contacting us and I'd be happy to assist.

To offer a content template selection field in a post form, you'll need to use a generic field, for example:
( ref: https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_generic_field )


[cred_generic_field type='radio' field='select-template']
{
"required":1,
"default":[],
"options":[{"value":"123","label":"Template 1"},{"value":"456","label":"Template 2"}]
}
[/cred_generic_field]

Note: In the generic field's options attribute, replace the values and labels, to match your content template IDs and titles.

Next, you'll need a custom function attached to the "cred_save_data" that gets the selected template's ID from the generic field and sets it as the "_views_template" custom field value:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )


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']==12345)
	{
		if (isset($_POST['select-template']))
		{
			update_post_meta( $post_id, '_views_template', $_POST['select-template'] );
		}
	}
}

Note: You'll replace '12345' with your actual form's ID.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2316215

a.R

Ok, this seiems to work. Thanks 🙂

And how set that radio buttons to the CURRENT page template when loading a CRED form to EDIT a post?

Thank you! 🙂

#2316349

Thanks for the update and glad that it worked.

Assuming that you're using the edit form on the same page/post that is being edited, you'll need a custom shortcode that returns the current post/page's assigned content template ID:
( ref: https://toolset.com/documentation/programmer-reference/views-api/#has_wpv_content_template )


add_shortcode('get_current_CT_ID', 'get_current_CT_ID_func');
function get_current_CT_ID_func() {
	global $post;

	$has_ct_assigned = has_wpv_content_template( $post->ID );
	if ( $has_ct_assigned > 0 ) {
		return $has_ct_assigned;
	} 
}

After that, you can use that shortcode in the default attribute of the generic field:


[cred_generic_field type='radio' field='select-template']
{
"required":1,
"default":"[get_current_CT_ID]",
"options":[{"value":"123","label":"Template 1"},{"value":"456","label":"Template 2"}]
}
[/cred_generic_field]

As a result, when the edit form will load, the currently assigned content template will be automatically selected in the generic field.

#2316547

a.R

My issue is resolved now. Thank you!

GREAT SUPPORT, THANK YOU, WAQAR!

This should be native part of Toolset, it´s so usefull!