[wp-hackers] action on change of role only

Sergey Biryukov sergeybiryukov.ru at gmail.com
Wed Jul 14 01:23:32 UTC 2010


You can try something like this:

function change_role_notify($meta_id, $object_id, $meta_key, $_meta_value) {
	$user = new WP_User($object_id);
	if ( $meta_key == $user->cap_key ) {
		$previous_user_role = array_shift( $user->roles );
		$current_user_role = array_shift( array_keys($_meta_value) );
		...
	}
}
add_action('update_user_meta', 'change_role_notify', 10, 4);

--
Sergey



On Wed, Jul 14, 2010 at 4:36 AM, Andrew Gray <andrew at graymerica.com> wrote:
> I am working on a plugin to automatically notify users when the admin changes their role from pending to subscriber.
>
> We were having some emails not arrive to activate and this is a way to streamline the activation process.
>
> Everything works fine with the hook in to the following action
>
> add_action('set_user_role', 'change_role_notify' , '' , '2');
>
> change_role_notify is my function, the action was added based on this trac ticket http://core.trac.wordpress.org/changeset/12028
>
> I am able to get the user id and the new role in the function and fire off the email.  I can not figure out how to get the previous role to compare.
>
> The issue I am having is the action is fired every time any part of the user information is edited.
>
> For example, if an admin changes the password,  the action is fire off, even if the role remains the same.
>
> Does anyone have any idea how to get the previous role to see if it changed before the action in the hook or if there is another action I should attach to,
>
> thanks,
>
> Andrew
>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list