Change WordPress user role based on myCRED points
-
I want to change WordPress user role based on myCRED points (except for admin role), however despite my attempts my admin user role also gets changed to editor one. What is missing here? OR Is there any better approach? Here is the code I am using:
<?php add_filter('mycred_add', 'check_for_role_change', 10, 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; // Get amount $amount = $request['amount']; // Role change values $roles = array( 'author' => 201, 'editor' => 100001, 'administrator' => 10000001 ); // Get users current balance $current_balance = $mycred->get_users_cred($request['user_id']); // Users balance once the requested amount is added $current_balance = $current_balance + $amount; $user = new WP_User($request['user_id']); $role = array_shift($user->roles); if (empty($user)) { return FALSE; } foreach ($roles as $role => $min) { if ($current_balance >= $min) { if (!empty($user)) { if ($role != 'administrator') { wp_update_user(array( 'ID' => $request['user_id'], 'role' => $role )); } } } } return $reply; } ?>
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Change WordPress user role based on myCRED points’ is closed to new replies.