Skip Navigation

[Resolved] Single select taxonomy terms in form

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

Problem: I would like to include a single select field for selecting a taxonomy term in CRED. The taxonomy is flat, not hierarchical.

Solution: CRED does not support a single select field for flat taxonomies, so you can either convert the taxonomy to a hierarchical taxonomy or you can use a generic field and the CRED API.

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field

This support ticket is created 6 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 2 voices.

Last updated by ruudd 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#780605
Screenshot 2018-05-01 at 21.01.02.png

I am trying to:

Allow a submitter to choose amongst category values (category A, category B, or category C) by using radio buttons or a drop down. Currently a submitter can enter any value in a free text format (see attached screenshot; it is named Categorie).

This is the code in the CRED form:

<div class="form-group">
<label>Categorie</label>
[cred_field field='adverts' class='form-control' output='bootstrap']
</div>

Link to a page where the issue can be seen:
hidden link

I expected to see:
The pre-defined category values I already defined as a taxonomy.

Instead, I got:
A free form entry field that allows submitters not to choose amongst the values that I defined, but instead submit any value.

#781334

Hi, the cred_field shortcode accepts different attributes that control the design style of the form element. Radio is not an option for a taxonomy field, but a single select field can be shown instead. Try this:

[cred_field field='adverts' display='select' single_select='true' class='form-control' output='bootstrap']
#788355
cred-form-cvah.png

Hello Christian,

A drop-down, single select would be perfect. I applied the shortcode (see screenshot), unfortunately to no avail.
You need login credentials to have a look?

Kind regards,
Ruud

#788812

In the URL bar of the screenshot you provided, the form ID is 3531. On the site, I can inspect the markup and see the form ID is 3530. Is it possible you're editing a different CRED form in wp-admin?

#789484
cvah-credform2.png

But when I change the label Categorie to CategorieII in the concerning form, it is changed as I would expect, as you can witness here: hidden link

Moreover, I only made 3 forms so far; see attachment.

Kind regards,
Ruud

#803381

Okay I see, I was looking at the Layout ID in the screenshot. Sorry for the confusion. Yes, please provide login credentials here so I can take a look. Thanks!

#804815

Sorry but this login is not working for me. Can you check the credentials once more?

#804938

Wasn´t working for me either...sorry about that!

#805454

Okay I see, this is a flat taxonomy. The "display" setting only applies to hierarchical taxonomies, so it's not possible to change the style of this taxonomy field. I think you could accomplish something similar with a generic select field. If there are just a few options, you can set them manually using the generic field wizard.
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

Or if there are many terms, you can use a View to provide the select field options. The value of each option should match the term slug. This is a bit more complex, but another ticket discusses this approach in detail:
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms
Instead of using the post ID in the loop, you would use the term slug.

Then use the CRED API to save the selected term when the form is submitted. Here's an example where the generic select field has the slug generic-select-term:

add_action('cred_save_data', 'set_term_from_generic_select',100,2);

function set_term_from_generic_select($post_id, $form_data) {
  $forms = array( 12345 );
  if( in_array( $form_data['id'], $forms) ){
    $taxonomy = 'adverts';
    $tag = $_POST['generic-select-term'];
    wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
}

Replace 12345 with the numeric ID of this form. Let me know if you have questions about this approach and I can offer some assistance.

#808805

Thanks for the help and patience, Christian,

I abandoned the flat taxonomy and got it working.

Kind regards,
Ruud