Last active
July 26, 2017 11:20
-
-
Save gabrielmerovingi/0e7446e913e737dafab5 to your computer and use it in GitHub Desktop.
Change a users WordPress role based on their current myCRED balance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Hello,
I can make it to work with wordpress Default roles like 'contributor', 'author', 'editor', 'administrator, and I use the plugin of "Capability Manager Enhanced" to create a new role named "Mycredlevelroleone" in our Multisites, and set 'Mycredlevelroleone' => 100000, and it does not work.
How to make it support the custom roles please?
Thanks.
Alex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, where should I put this code please? thanks a lot.
Alex
Thanks, I got message at https://mycred.me/code-snippets/change-role-based-on-balance/
: The above code goes into your theme's function.php file.
Thanks again.
Alex