Skip Navigation

[Resolved] How to force a Taxonomy in a form and not let the user chose

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

Problem: When submitting a CRED form to create a new post, I would like to automatically assign a specific taxonomy term without allowing the user to select or unselect it. I would like to assign different taxonomy terms to the post depending on the page where the CRED form is found.

Solution: Use the CRED API to manually set terms to your post depending on the post ID where the form is placed.

// automatically assign a slug to all posts created by CRED
add_action('cred_save_data', 'set_term_for_cred_post',10,2);
function set_term_for_cred_post($post_id, $form_data) {
  // array of cred form IDs and corresponding term slugs
  $array = [
    '1234' => 'term-slug-1',
    '5678' => 'term-slug-2'
  ];
  $taxonomy = 'my-taxonomy-slug';
  $tag = array( $array[$form_data['id']] );
  wp_set_object_terms( $post_id, $tag, $taxonomy, true );
}

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-api/

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

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

Last updated by eci38.fr 7 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#527529

Dear support,
I want to implement petition forms on my site where each person signing the petition will be created as a CPT and the different petitions names are defined as a taxonomy value so that we can sort and display persons based on the taxonomy (i.e. petition they signed). One person can signed several petitions, so may have several taxonomy values.

I have created the Petition Taxonomy, the signing user CPT and in the form I expect to be able to force the Taxonomy without it is asked to user to chose with a dropdown list or checkbox field.

I didn't find this capability in the Toolset. Could you help me to implement this capability easily as I'm not PHP expert and I was expecting it is easy with the toolset to create such feature.

Thank you for your help.

#527696

Hi, if I understand you correctly, you want to set the taxonomy of a post, created by CRED, without letting the user choose it. This can be accomplished with a hidden field and some custom PHP code. In order to implement it, I will need the following information:
- Login credentials for your wp-admin area, and a URL where I can login
- FTP access to your server, so I can modify your site files
- The name of the CRED form
- How should I determine the petition taxonomy term for each form, and where will the forms be placed?

I will enable private reply fields here so you can share your credentials securely.

#527972

Hi, I'm not able to log in to your wp-admin area using the credentials you provided. Can you double check for me? I will enable private reply fields again so you can share an update if necessary.

#528486

Ok great, sorry for the delay but I had a difficult time cloning your site. Eventually I was able to get the WP Clone plugin to work for me. You were very close to having this working, but you must use the wp_set_object_terms function for custom post types. Here's some code for you:

add_action('cred_save_data', 'set_petition',10,2);

function set_petition($post_id, $form_data) {
  // array of cred form IDs and corresponding petition slugs
  $array = [
    '3649' => 'appel-ouvreurs-2017',
    '1234' => 'appel-des-jeunes'
  ];
  $taxonomy = 'petition';
  $tag = array( $array[$form_data['id']] );
  wp_set_object_terms( $post_id, $tag, $taxonomy, true );
}

Now whenever you add a new petition, you must add a new item to $array. The key will be the CRED form ID, and the value will be the slug of the petition term. In the example above, I added appel-des-jeunes using the ID 1234 just to show you how that would look. You can delete it.

Please implement this code and let me know how it works for you.

#528719

Hi Christian,
This is great thank you, very good to associate all CRED forms IDs with taxonomy slug in a table. I didn't understand correctly how it worked this $array I could find in some examples... now it works fine!
Theres is still an issue I have is that when I create a new form, it is a form to create new post ('Signataire') or to update it. But I can use only one form because when people are going to sign they use the form on page or post and it is not possible to know before if this 'Signataire' already exist in the database or if it is a new one before he has completed and sent the form for validation.
I would expect that from CRED form it could detect on the fly if the post 'Signataire' is new or it is to update an existing one.
Why should I create one CRED form for each specific case while I don't have this capability a priori...

Maybe this is not possible and then I will have to change the strategy for this. Do you have any hint for this?

Anyway thank you for your good support!
Christophe

#529430

Hi, CRED is not smart enough to know whether it should display a "New Signataire" or "Edit Signataire" form to a user, and requires separate forms to create and edit. To accomplish this type of conditional display, you can combine CRED and Views.

Create a new View of Signataires that are children of the current Petition and whose author is the current user. If any items are found, display your Edit CRED form for the Signataire by including the form shortcode in your Loop Output editor (just before the <wpv-loop> is fine). If no items are found, display your New CRED form shortcode instead of "No items found".

Why should I create one CRED form for each specific case while I don't have this capability a priori...

Maybe this is not possible and then I will have to change the strategy for this. Do you have any hint for this?
I'm not quite sure I understand what you mean. Would you like to use only one CRED edit form and only one CRED new form for all petitions? If so, you can include a generic hidden field that captures the current petition ID, and use the petition ID instead of the form ID in your $array keys. This would allow you to use only two CRED forms: one to create Signataires for all petitions, and one to edit Signataires for all petitions.
More info about generic fields:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

Feel free to correct me if I have misunderstood. Thanks!

#529684

Hi Christian,
Thanks for your hints. I'll try to find a solution on my side. If not I'll open a new ticket for this specific need. But for the moment I'd say my initial problem is solved! Thanks again for your help.
Christophe