Hey y’all, I’m having issues with the following code (in functions.php) I found online. I can’t find the error, to me it sounds logic. It’s meant to change the wp user role depending on the current Mycred balance but somehow it doesn’t work. I assume it doesn’t actually call the function at all but idk why. Does someone have an idea? Also tried with hook ‘wp\_login’ instead of ‘Mycred\_add\_finished’ but that gives a critical error on login (at least, here it does something)
​
Thanks already 🙂
​
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(
‘subscriber’ => 0,
‘contributor’ => 1
);
// 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;
}
Code looks alright to me, i’d assume the reason it’s failing on a different hook is the omission of the function parameters.
You might have to step through the different lines of the code when the filter is expected to run, to see if it halts prematurely, or returns something unexpected.