Wordpress user roles - gravity form User Registration - conditionally add_role -


my wordpress site has 2 types of user. when sign up, nominate user type, , want assign 1 of 2 roles, depending on user type are.

my user registration page has radio button, new user chooses whether between 2 types of registration. lets call registration types 'cat' , 'dog' purpose of discussion.

i have added cat/dog radio button gravity form, user can select 'dog' - radio button defaults 'cat'. field 'registeras'.

gravity form user registration allows me set role on new user. choose 'dog' new registrations

the gravity form used gather new user data has confirmation redirect them cat-or-dog page: confirmation

the cat-or-dog page has template assigned - let's call 'cat-or-dog.php'. contains code:

<?php /* template name: cat or dog */ if (isset($_get['regas'])) { if ($_get['regas']=='dog') { global $current_user; var_dump($current_user->roles); $current_user->add_role('dog'); echo "after:"; var_dump($current_user->roles); die(); 

i expect output show roles changing dog cat. instead, output following:

array(1) { [0]=> string(6) "cat" } after:array(1) { [0]=> string(6) "cat" }

so appears add_role nothing!

can correct code? or perhaps there way assign role conditional on radio button during user registration.

i want use gravity forms user registration plug in, used throughout site , not collector of plugins.

note have not removed 'cat' role yet - first wanting overcome add_role problem. work on removing role.

the gform_user_registered hook provided gravity forms code implement this.

https://www.gravityhelp.com/documentation/article/gform_user_registered/

at top of theme functions.php file, included code:

function change_cat_dog_role( $user_id, $user_config, $entry, $user_pass ) {     $question1 = rgar( $entry, '8' ); //field id 8       if ( $question1 == 'dog' ) {         //update role         $user_obj = new wp_user( $user_id );         $user_obj->add_role( 'dog' );     } }  add_action( 'gform_user_registered', 'change_cat_dog_role', 11, 4 ); 

note add role 'dog' lower case, though role proper case 'dog'.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -