Home › Forums › Plugin Support › upgrade user role
- This topic has 13 replies, 2 voices, and was last updated 12 years ago by mca.
-
AuthorPosts
-
November 7, 2012 at 10:29 pm #2434November 7, 2012 at 10:45 pm #2436AllboundMember
i’ve tried hooking the process, but i can’t get the hook to actually fire. and i’ve copied/pasted from the docs… this is my morning’s project so i’ll post back here in a few if i solve anything.
November 7, 2012 at 11:17 pm #2438mcaMemberthank you idstm,
i create a new profile form and i’m looking for the name of the filter hook that i can use to filter my data before send the form. I use this hook for my REGISTRATION “user_meta_pre_user_register”. Where ca i find that ?November 7, 2012 at 11:17 pm #2439mcaMemberthank you idstm,
i create a new profile form and i’m looking for the name of the filter hook that i can use to filter my data before send the form. Where can i find that ?(I use this hook for my REGISTRATION “user_meta_pre_user_register”.)
November 7, 2012 at 11:44 pm #2440mcaMemberIn fact it’s the good hook but i don’t understand while i can’t update the field role ?
the filter hook (thank you toddz70 !) :add_filter('user_meta_pre_user_register', 'tz_registration_role');
function tz_registration_role($userData){
if( isset($userData['cacap'])){
// registration for demande
$userData['role'] = 'demandeur_adhesion';
} else {
// registration for membre
$userData['role'] = 'membre';
}
return $userData;
}thank’s for your help,
mcaNovember 7, 2012 at 11:56 pm #2442AllboundMemberi can’t get the user_meta_pre_user_register to ever work. so instead i use:
add_action(‘user_register’, ‘myfunctionname’);
in this case, however, you want to update the user role for an existing user, right?
in that case i’d do something simple like this: ask them the question you care about … like “membership level”. have that be a custom field on the profile. and then run an action on user_update to double check their role based on that field value.
add_action( ‘profile_update’, ‘my_profile_update’ );
function my_profile_update( $user_id, $old_user_data ) {
// get the user_meta field you care about here// get the user role
//remove_role( ‘whatever current role is’ );
// add_role( ‘whatever you want it to be’ );
return true;
}November 8, 2012 at 12:18 am #2444mcaMemberthank you !
I try your hook but i can’t remove the role.
My function checks for the existence of the field “signtomembre” in the submitted data from the form, and if found, we know it’s a upgrade from signataire to member. So in my functions.php :add_action( 'profile_update', 'my_profile_update' );
function my_profile_update($user_id) {
if( isset($userData['signtomembre'])){
remove_role( 'signataire');
add_role( 'membre');
}return true;
}what is wrong ?
November 8, 2012 at 1:01 am #2445AllboundMembersorry, i should have explained out the add_role remove_role a little better…
you need to get the user object, then call the action on that…
$user = get_userdata($user_id);
$user->remove_role(‘signataire’);
$user->add_role(‘membre’);November 8, 2012 at 2:03 am #2446mcaMembersorry there is still a mistake : i put this hook in the function.php file and i test it on a profile form create with user meta pro…
add_action( 'profile_update', 'my_profile_update' );
function my_profile_update($user_id) {
// get the user_meta field you care about here
if( isset($userData['signtomembre'])){
//$user = $userData['user_id'];
$user->remove_role('signataire');
$user->add_role('membre');
}
// get the user rolereturn true;
}November 8, 2012 at 2:27 am #2447AllboundMemberi was just giving you some items to use. but you’d need to flush it out… to something like this…
add_action( ‘profile_update’, ‘my_profile_update’ );
function my_profile_update($user_id) {
// get the user object
$user = get_userdata($user_id);
// get the custom field you want to use to store your role
// change ‘role’ to your key value of your form field
$role = get_user_meta($user_id,’role’,true);// does it exist?
if($role != ”)
{
$user->remove_role(‘signataire’);
$user->add_role(‘membre’);
}
return true;
}you could actually store 2 things on the form… old and new role. you have lots of flexibility form here.
November 8, 2012 at 2:45 am #2448mcaMember// get the custom field you want to use to store your role
why ?
i want to store the role in the data base filed wp_capabilities like that : a:1:{s:10:”signataire”;s:1:”1″;}// does it exist?
no there ins no field “role” on my form, i just test if ( current_user_can( ‘signataire’ ) ) i change the role.i try that (always wrong !) :
add_action( 'profile_update', 'my_profile_update');
function my_profile_update() {
if ( current_user_can( 'signataire' ) )
{
$current_user->remove_role('signataire');
$current_user->add_role('membre');
}
}
Can you explain me the logical processus to do what i want ton do ?November 8, 2012 at 2:49 am #2449mcaMember// get the custom field you want to use to store your role
why ?
i want to store the role in the data base filed wp_capabilities like that : a:1:{s:10:\”signataire\”;s:1:\”1\”;}// does it exist?
no there ins no field \”role\” on my form, i just test if ( current_user_can( \’signataire\’ ) ) i change the role.i try that (it work !) :
function tgm_custom_profile_redirect() {
if ( current_user_can( 'signataire' ) )
{
global $current_user;
get_currentuserinfo();
$current_user->remove_role('signataire');
$current_user->add_role('membre');
// wp_redirect( trailingslashit( home_url() ) );
// exit;
}
}
thank for your help !
mcaNovember 8, 2012 at 2:56 am #2450AllboundMemberwell, you would need to know when to change this user’s role. you would not want to just update any user profile to a different role, any time they updated their profile.
so, how would you know when to update this role? only when the user fills out one specific form, right?
if so, then you could just use a custom field on the form as a trigger to update the user role.
that’s all i’m thinking is done here. but it could really work in a million different ways.
November 8, 2012 at 1:43 pm #2453mcaMemberYes you’re right i have to think about the way i want to do this :)In fact i use the form “both” like a registration form and a profile form…
Maybe it’s not a good idea ?
best regard,
mca -
AuthorPosts
- You must be logged in to reply to this topic.