Advertisement
Guest User

Assign role based on GF field choice

a guest
Oct 30th, 2015
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2. // Assign user role based on field choice
  3. add_action("gform_user_registered", "add_custom_user_meta", 10, 4);
  4. function add_custom_user_meta($user_id, $config, $entry, $user_pass) {    
  5.     //Assign the role from form entry 6 on registration
  6.     $user_obj = new WP_User( $user_id );
  7.     $user_obj->set_role( $entry[6] );
  8.  
  9. }
  10.  
  11. // Update user role based on field choice
  12. add_action( 'gform_user_updated', 'change_role', 10, 4 );
  13. function change_role( $user_id, $user_config, $entry, $user_pass ) {
  14.   $user_obj = new WP_User( $user_id );
  15.   if(!empty($entry[6])) {  
  16.   //Assign the role from form entry 6 on registration    
  17.         $user_obj->set_role( $entry[6] );
  18.       } else {
  19.   //If nothing is chosen, keep the old role
  20.         $user_obj->set_role($user_obj->roles[0]);
  21.       }
  22. }
  23. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement