Changing Wordpress User Roles for Patrons

I have been doing my research and come up with what I think should work and was hoping more knowledgeable people could check my code for me. The intention is to assign a Wordpress user role for patrons and remove it from them when they end their support.

Also, I’m not positive where in my Wordpress code this belongs. (I know enough code to get me in trouble, but not enough to be 100% proficient.)

if ( !Patreon_Wordpress::isPatron( $user ) )
{
// Is not a patron
$user = wp_get_current_user();
if ( in_array( ‘patron’, (array) $user->roles ) )
{
//The user has the “patron” role
$u->remove_role( ‘patron’ );
}
$user = wp_get_current_user();
if ( !in_array( ‘patron’, (array) $user->roles ) ) {
//The user does not have the “patron” role
$u->add_role( ‘patron’ );
}
}

The logic in there should work. You may need to run tests and see if it works as you intend to. This code should only run on registration/login, and not all the time.

To do that, you should put that in a function inside a custom plugin of yours, and hook that function to below hooks in the Patreon WP plugin code:

patreon_action_after_wp_logged_user_is_updated

patreon_do_action_after_user_logged_in_via_patreon

patreon_do_action_after_new_user_created_from_patreon_logged_in

patreon_do_action_after_existing_user_from_patreon_logged_in

You can find many tutorials via google as well.

Seems like a very useful feature since Paid Membership Pro plugin has an add-on to assign specific user role to each member tier too.

I’d like to have new account created automatically by logging in with Patreon to be assigned with corresponding role.

If the code at the beginning of this thread does that can you tell me more about how to add it to the plugin?

Thank you

Yes that code could do that. You shouldnt add it to the plugin though. Googling about how to add custom code to your WP site could help.