Skip Navigation

[Resolved] Automatically set a taxonomy term per CRED form

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

Problem: I have 6 Forms that create custom posts. For each Form, I would like to automatically set one term in a custom taxonomy when the post is published.

Solution: Use the cred_save_data API to programmatically set a term specific to each Form for the published posts:

/* - Toolset support - for ticket #1691935 - */
 
// Bewerber: 13 = VR, 399 = Berater, 403 = Sportler; Inserate: 255 = VR, 584 = Berater, 586 = Sportler
 
add_action('cred_save_data', 'ts_set_industry_term',100,2);
function ts_set_industry_term($post_id, $form_data) {
  $forms = array( 13, 399, 403, 255, 584, 586 );
  $terms = [
    '13' => 'vr-mandate',
    '399' => 'berater-innen',
    '403' => 'sportler-innen',
    '255' => 'vr-mandate',
    '584' => 'berater-innen',
    '586' => 'sportler-innen',
  ];
  $taxonomy = 'profil';
  
  // -- you should not edit anything below this line
  
  if( in_array( $form_data['id'], $forms ) ) {
    $tag = array( $terms[$form_data['id']] );
    wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
};

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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.

This topic contains 5 replies, has 2 voices.

Last updated by marcialB 1 year ago.

Assigned support staff: Christian Cox.

Author
Posts
#1691935

Hi there

I have two custom post types (company, supplier) with a taxonomy assigned. There a three options in that taxonomy (industry X, industry Y, industry Y).

I have six CRED forms - one for each combination of CPT and taxonomy (e. g. "company" in "industry X"). I want the taxonomy to be assigned correctly. I have included the taxonomy field in the CRED form. However, there is no option to predefine what taxonomy should be selected. I tried the following code:

document.querySelector('option[value="17"]').selected = true

That did work when I only had one form on a page. But we now have two (one for each CPT). For the second form, the code doesn't work anymore. Is there a way to adjust that code above?

If not, is there a way to add a code to the functions.php that when submitting a form automatically sets the taxonomy based on the CRED form ID?

Thanks for your help!

#1692251

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hello, you can programmatically assign a taxonomy term or terms using the cred_save_data PHP hook in your functions.php file. The PHP API approach is more reliable than setting it with JavaScript. I would remove the taxonomy term field from the Forms so there is no confusion about which term was selected vs. which terms are actually applied programmatically. You'll need to know the 6 Form IDs, the 3 industry term slugs, and the industry taxonomy slug to update the code example. Here is the full example code:

/* - Toolset support - for ticket #1691935 - */
add_action('cred_save_data', 'ts_set_industry_term',100,2);
function ts_set_industry_term($post_id, $form_data) {
  $forms = array( 1, 2, 3, 4, 5, 6 );
  $terms = [
    '1' => 'industry-term-x-slug',
    '2' => 'industry-term-x-slug',
    '3' => 'industry-term-y-slug',
    '4' => 'industry-term-y-slug',
    '5' => 'industry-term-z-slug',
    '6' => 'industry-term-z-slug',
  ];
  $taxonomy = 'industry-taxonomy-slug';

  // -- you should not edit anything below this line

  if( in_array( $form_data['id'], $forms ) ) {
    $tag = array( $terms[$form_data['id']] );
    wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
}

You should make the following modifications. Replace these numbers with a comma-separated list of the 6 Form IDs, in no particular order:

$forms = array( 1, 2, 3, 4, 5, 6 );

Next, in no particular order, update the terms array. Each row here should represent one Form and the term you would like to programmatically assign to the posts it creates. Replace the Form IDs on the left and the term slugs on the right as needed, keeping the single quotation marks in place, so that each Form ID on the left has the corresponding desired term slug on the right side of =>:

$terms = [
    '1' => 'industry-term-x-slug',
    '2' => 'industry-term-x-slug',
    '3' => 'industry-term-y-slug',
    '4' => 'industry-term-y-slug',
    '5' => 'industry-term-z-slug',
    '6' => 'industry-term-z-slug'
  ];

Finally, replace industry-taxonomy-slug here with the slug of the custom taxonomy:

$taxonomy = 'industry-taxonomy-slug';

Let me know if you have questions about this.

#1692461
toolset.PNG

Hi Christian

Thanks a lot for your detailed explanation. I adjusted the code as you described. But it doesn't seem to work. I'll attach a screenshot of how I got the values for the taxonomy and terms. The IDs of the forms are correct because I use those already for a different code that creates dynamic post titles.

This is my current code (please note that the "industry" I used in the explanation corresponds to the taxonomy "profil"):

/* - Toolset support - for ticket #1691935 - */

// Bewerber: 13 = VR, 399 = Berater, 403 = Sportler; Inserate: 255 = VR, 584 = Berater, 586 = Sportler

add_action('cred_save_data', 'ts_set_industry_term',100,2);
function ts_set_industry_term($post_id, $form_data) {
  $forms = array( 399, 403, 12, 584, 586, 255 );
  $terms = [
    '13' => 'vr-mandate',
    '399' => 'berater-innen',
    '403' => 'sportler-innen',
    '255' => 'vr-mandate',
    '584' => 'berater-innen',
    '586' => 'sportler-innen',
  ];
  $taxonomy = 'profil';
 
  // -- you should not edit anything below this line
 
  if( in_array( $forms, $form_data['id'] ) ) {
    $tag = array( $terms[$form_data['id']] );
    wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
}

I don't see anything wrong with that. Do you have an idea why this is not working for me?

Thanks!

#1693155

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

The code you pasted here isn't identical to the code in the screenshot, so maybe there is a transposition error? In the code you pasted, the Form ID 12 is in the $forms array, but the ID 13 is used in the $terms array. One of those is wrong - they should be identical. But otherwise it should be working correctly for the other Forms / terms. If it's not working as expected, I'll need to take a closer look. Can I get a login? Where can I find the Form on the front-end of the site?

#1695829

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Got it - there was a typo in my code in the in_array conditional, and I've corrected that in my snippet above. I tested and created this post: hidden link
The Berater/innen term was added successfully. Can you confirm we're working correctly across the Forms now?

Here is the updated code for reference:


/* - Toolset support - for ticket #1691935 - */

// Bewerber: 13 = VR, 399 = Berater, 403 = Sportler; Inserate: 255 = VR, 584 = Berater, 586 = Sportler

add_action('cred_save_data', 'ts_set_industry_term',100,2);
function ts_set_industry_term($post_id, $form_data) {
  $forms = array( 13, 399, 403, 255, 584, 586 );
  $terms = [
    '13' => 'vr-mandate',
    '399' => 'berater-innen',
    '403' => 'sportler-innen',
    '255' => 'vr-mandate',
    '584' => 'berater-innen',
    '586' => 'sportler-innen',
  ];
  $taxonomy = 'profil';
 
  // -- you should not edit anything below this line
 
  if( in_array( $form_data['id'], $forms ) ) {
    $tag = array( $terms[$form_data['id']] );
    wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
};
#1697627

I tested it successfully with all six forms. Thank you very much for your help!