Cybercli Posted August 10, 2022 Share Posted August 10, 2022 The following code works as is to promote a user role when a points balance minimum is met, but I also need the role to change back to prior role if the points balance falls below the minimum. For example if points balance drops to 900 then the new role will be set to 'contributor'. /** * Promote Based on Balance * Changes a users role based on their myCRED balance. * @version 1.0.4 */ add_filter( 'mycred_add_finished', '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( 'contributor' => 100, 'author' => 1000, 'editor' => 10000, 'administrator' => 100000 ); // 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' => $new_role ) ); return $reply; } Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/ Share on other sites More sharing options...
ginerjm Posted August 10, 2022 Share Posted August 10, 2022 (edited) I may be missing something but it appears that your function will do what you want already. You are testing the new balance against the min values for each category. Whatever min value is identified as what s/b a new role you are assigning at the end already. Edited August 10, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/#findComment-1599223 Share on other sites More sharing options...
Cybercli Posted August 10, 2022 Author Share Posted August 10, 2022 I am using myCRED to distribute points and have been testing it. It only works to promote a user from Subscriber to Author after I add points to that user. But when I deduct points back to below the minimum the user role remains at Author. Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/#findComment-1599224 Share on other sites More sharing options...
Cybercli Posted August 10, 2022 Author Share Posted August 10, 2022 Should I be adding an elseif function? Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/#findComment-1599225 Share on other sites More sharing options...
ginerjm Posted August 10, 2022 Share Posted August 10, 2022 (edited) Add some echos to diagnose what is happening . What does that extract line do for you here? Change new role to be a blank (or assign 'contributor') to start. Then assign the new role when your test finds one that fits. If new role is not blank at the end of the loop then post it. Edited August 10, 2022 by ginerjm new suggestion Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/#findComment-1599226 Share on other sites More sharing options...
benanamen Posted August 10, 2022 Share Posted August 10, 2022 (edited) You are over complicating the whole thing. Simply determine the role on the fly instead of storing it. Edited August 10, 2022 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/#findComment-1599227 Share on other sites More sharing options...
Barand Posted August 10, 2022 Share Posted August 10, 2022 With a couple of db tables like this Table: user Table: role +---------+----------+--------+ +---------+---------------+-----------+------------+ | user_id | username | points | | role_id | role_name | point_min | points_max | +---------+----------+--------+ +---------+---------------+-----------+------------+ | 1 | John | 66 | | 5 | - | 0 | 100 | | 2 | Paul | 101 | | 6 | Contributor | 101 | 1000 | | 3 | George | 3000 | | 7 | Author | 1001 | 10000 | | 4 | Ringo | 200000 | | 8 | Editor | 10001 | 100000 | +---------+----------+--------+ | 9 | Administrator | 100001 | 999999999 | +---------+---------------+-----------+------------+ Then a simple query SELECT username , rolename FROM user u JOIN role r ON u.points BETWEEN r.points_min AND r.points_max; does the job for you +----------+---------------+ | username | rolename | +----------+---------------+ | John | - | | Paul | Contributor | | George | Author | | Ringo | Administrator | +----------+---------------+ 3 Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/#findComment-1599230 Share on other sites More sharing options...
Barand Posted August 10, 2022 Share Posted August 10, 2022 PS If you really want to do it in PHP then you can apply the same method to an array $thresholds = array( '-' => [0, 100], 'contributor' => [101, 1000], 'author' => [1001, 10000], 'editor' => [10001, 100000], 'administrator' => [100001, 999999999] ); echo getUserRole(50000, $thresholds); //--> editor function getUserRole($current_balance, $thresholds) { foreach ( $thresholds as $role => $range ) { if ( $range[0] <= $current_balance && $current_balance <= $range[1] ) { return $role; } } return false; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/315164-need-this-code-to-demote-user-role-if-points-fall-below-minimum/#findComment-1599232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.