Skip Navigation

[Resolved] How to choose which content template to use with a CRED dropdown

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

Problem:
How to assign selected content template dynamically to posts created using CRED form

Solution:
You can use CRED generic field to build and display the content template you want to offer for user selection.

_views_template" is the meta key that holds the assigned content template value. You can find proposed solution with the following reply.
=> https://toolset.com/forums/topic/how-to-choose-which-content-template-to-use-with-a-cred-dropdown/#post-593738

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

This support ticket is created 7 years, 1 month 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 4 replies, has 2 voices.

Last updated by tylerR-3 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#592923

Hi there, we are creating a CRED form to make it easier for our authors to add content to our site from the front-end. We want to build two content template designs that can each be used for our posts. We then want our authors to choose via dropdown or radio button on the form either a 'complex post' or 'simple post'. Depending on which option they choose, the post will display with a different layout. How do we add a content template choice in our CRED form?

Thanks for any help.

#593254

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

There is no such feature exist to add content template dropdown to your CRED form but Yes, using some custom programming you can achieve this.

You should try to add dropdown select box using CRED generic field with your content template options to your CRED form.
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

Now, you can display different template for your post type using the view's filter hook wpv_filter_force_template :
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

For example:

//Show a specific Content Template applied to a given post for not logged in visitors and in every place where this post appears:
add_filter( 'wpv_filter_force_template', 'prefix_fixed_content_for_visitors', 99, 3 );
function prefix_fixed_content_for_visitors( $template_selected, $id, $kind ) {
    if ( !is_user_logged_in() && $id == 345 ) { // if the user is not logged in and is trying to view the post with ID 345
        $template_selected = 123; // assign a fixed Content Template with ID 123 that contains a static text
    }
    return $template_selected;
}

You should adjust the the above filter code as per your requirement by checking which user selected which content template to display.

#593734

Hi there, I think maybe I wasn't clear enough on what I was trying to do. I have 2 content templates made for posts. I haven't assigned them to the 'post' post type because I want that to be chosen on the form when our author creates the article. So for example, I tried this with general CRED radio buttons, but it didn't quite work:

[cred_generic_field field='views_template' post='post' type='radio' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[
{"value":"172","label":"Standard News"},
{"value":"181","label":"Creative News"}
]
}
[/cred_generic_field]

Those values and labels refer to my content templates that I made. What am I missing, or need to add to get this to work?

Thanks.

#593738

Minesh
Supporter

Languages: English (English )

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

When you say it's not working - you mean to say that selected radio button value not saved to database - correct?

Well - "_views_template" is the meta key that holds the assigned content template value.

So, Please try to use the following code:

[cred_generic_field field='_views_template' post='post' type='radio' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"persist":1,
"options":[
{"value":"172","label":"Standard News"},
{"value":"181","label":"Creative News"}
]
}
[/cred_generic_field]

As you can see I've added attribute "persist":1 to above CRED generic field shortcode, this attribute will help you to save the Generic field value to database.

#593762

Beauty! Works perfect, thank you!