Skip Navigation

[Resolved] Taxonomy based on user role

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

Last updated by Ian 5 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1212276

Ian

Tell us what you are trying to do?
I want to set a taxonomy term based on user role

I found this in your help and hope to modify it
https://toolset.com/forums/topic/fixed-taxonomy-category-values-in-form/

I have four taxonomy terms and there will be no more added: office, recreation, economic-development, fire-department.

What I am trying to do is if the user role is:
"Recreation Manager" the taxonomy is set to "recreation"
"Office Manager" the taxonomy is set to "office"
"Fire Manager" the taxonomy is set to "fire-department"
"Development Manager" the taxonomy is set to "economic-development"

how would I adjust the code provided in the example on the link provided

#1212514

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - You just need to apply the if condtion to check the current user role and assing the taxonomy term accordingly.

Please try to add the following code to your current theme's functions.php file
or
Toolset custom code section
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

For example:

add_action('cred_save_data', '',100,2);
function set_term_from_generic_select($post_id, $form_data) {
  global $current_user;
   
  if( $form_data['id'] == 9999 ){

    $current_role = $current_user->roles;

    $taxonomy = 'YOUR-TAXONOMY-SLUG';

    if ( in_array( 'recreation-manager', (array) $current_user->roles ) ) {
     $tag = "recreation";
    }else if ( in_array( 'office-manager', (array) $current_user->roles ) ) {
     $tag = "office";
    }else if ( in_array( 'fire-manager', (array) $current_user->roles ) ) {
     $tag = "fire-department";
   }else if ( in_array( 'development-manager', (array) $current_user->roles ) ) {
     $tag = "economic-development";
   }
   wp_set_object_terms( $post_id, $tag, $taxonomy, true ); 

  }
}

Where:
- Replace 9999 with your original form ID
- Replace YOUR-TAXONOMY-SLUG with your original taxonomy slug
- Replace the role slug if needed within the in_array argument

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1212664

Ian

I was still having problems with this but found a solution that works better with more flexibility for me here: https://toolset.com/documentation/adding-custom-code/require-at-least-one-checkbox-of-a-checkboxes-field-to-be-checked-in-a-toolset-form/