Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nadeem-khan/54904de0715a0fbbe800 to your computer and use it in GitHub Desktop.
Save nadeem-khan/54904de0715a0fbbe800 to your computer and use it in GitHub Desktop.
Change WordPress user role based on Mycred Points

Change WordPress user role based on Mycred Points:

add_filter( 'mycred_add', 'check_for_role_change', 99, 3 );
 function check_for_role_change( $reply, $request, $mycred ) {
// Make sure that if any other filter has declined this we also decline
if ( $reply === false ) return $reply;

// Exclude admins
if ( user_can( $request['user_id'], 'manage_options' ) ) return $reply;

extract( $request );

// Minimum balance requirement for each role
$thresholds = array(
 'author' => 201,
    'editor' => 100001,
    'administrator' => 10000001
);

	// Get users current balance
$current_balance = $mycred->get_users_balance( $user_id, $type );
$current_balance = $current_balance + $amount;

// Check if the users current balance awards a new role
$new_role = false;
foreach ( $thresholds as $role => $min ) {
	if ( $current_balance > $min )
		$new_role = $role;
}

// Change users role if we have one
if ( $new_role !== false )
	wp_update_user( array(
		'ID'   => $user_id,
		'role' => $role
	) );

return $reply;
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment