Change UM User’s Role Based on Mycred balance

[ad_1]

I’m trying to get this code to work that I’m building off of another with my limited knowledge of PHP. It’s not working and I’m not sure what I’m missing because the code looks valid to me.

**From what I’m seeing:**

* It should trigger when [mycred\_add\_finished]) returns true
* Find the user’s balance using the [mycred\_get\_users\_cred]) filter
* Check to see if the balance is greater than 10
* Change the (Ultimate Member) user’s role using the [set\_role]) method.

​

add_action(‘mycred_add_finished’, ‘um_change_role_based_on_mycred_points’, 10, 4);

function um_change_role_based_on_mycred_points($user_id, $amount, $type_key, $result) {
// Check if the myCRED points were added successfully
if ($result === true && $amount > 0) {
// Retrieve myCRED balance for the default point type
$balance = mycred_get_users_cred($user_id, $type_key);

// myCRED balance condition (greater than 10)
if ($balance > 10) {
// Assign new user role
UM()->roles()->set_role($user_id, ‘um_star-student’);
}
}
}

The UM set\_role method works when I test it directly on a user. But I’m at a loss. I probably missed something. Can someone point me in the right direction?

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer