Plugin Author
myCred
(@designbymerovingi)
Are we talking the default WordPress roles or roles customized though a role management plugin?
Both if possible. For example, in my own site, I have a few custom capabilities that I have assigned to custom roles that I have created.
Plugin Author
myCred
(@designbymerovingi)
I will need to look into how one could do this. Im guessing just as you change a users role while editing their profile in the admin area also change their roles once they reach a certain number of points.
You would need to setup two things:
1. Points Settings
How many points does a user need to get promoted? You will need to store this somewhere.
2. Hook into myCRED
Your best bet would be to hook in via the mycred_add filter which fires each time a user gains / looses points. You could each time check if they reached enough points to get promoted then promote them if they do.
I admit that I am a little bit vary about attempting this myself. Gabriel, do you have any plans to code a feature like this yourself for the next update?
Thank you for your time.
Plugin Author
myCred
(@designbymerovingi)
If everyone out there would use the default WordPress roles I could see it being implemented but the issue is that there are a lot of different role management plugins out there and each does things their own way. What I can promise is that I will look into it as soon as time permits and see what options are available.
Thank you very much. If it came to the case that the only reasonable way to do this is for it to use the default WordPress roles, then that is absolutely fine. I look forward to hear more about this whenever you find the opportunity too.
Plugin Author
myCred
(@designbymerovingi)
A very basic example:
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(
'contributor' => 100,
'author' => 1000,
'editor' => 10000
);
// 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;
foreach ( $roles as $role => $min ) {
if ( $current_balance >= $min )
wp_update_user( array(
'ID' => $request['user_id'],
'role' => $role
) );
}
return $reply;
}
Placed in your themes functions.php file, this snippet will change the users role if they reach the amount defined for each role. In it’s current state the user will be promoted for each limit they surpass. So if the user is a “subscriber” and all of the sudden gains 10000 points, they will be promoted 3 times in one go.
AMAZING! Allow me a couple of days working on other part of the site for integration until I try this code and see how it works. I will have questions.
Also, this plugin has probably the best support available, great job.
Plugin Author
myCred
(@designbymerovingi)
Just be careful with the code as it will change the role of everyone getting points even admins. It serves mainly as a starting block for what you would want. Let me know if you need any further assistance.
I see, thank you for informing me of this. Is it possible to make it not “downgrade” users to avoid admins/editors becoming contributors?
Plugin Author
myCred
(@designbymerovingi)
You could start off by checking if the user gaining / loosing points is an admin and if they are, bail:
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;
// Check for admins
// they tend to have the capability "delete_users" which we can check for
if ( user_can( (int) $request['user_id'], 'delete_users' ) ) return $reply;
// Get amount
$amount = $request['amount'];
// Role change values
$roles = array(
'contributor' => 100,
'author' => 1000,
'editor' => 10000
);
// 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;
foreach ( $roles as $role => $min ) {
if ( $current_balance >= $min )
wp_update_user( array(
'ID' => $request['user_id'],
'role' => $role
) );
}
return $reply;
}
I find capability checks to be easiest way to determine if a user is admin but of course if you have customized your user capabilities and roles this might not work well. For more information on what capabilities the default WordPress roles have, you can consult the WordPress codex.
I will try this code out and report back in the coming days. Millions of thanks Gabriel.
Just coming here to confirm that this works great! I have done many tests and can’t find any difficulties with it.
But I do have one final request if you don’t mind. Is it possible to add a check if the user has 5 published post, then continue, otherwise do not change role values.
All of my users are writers so I want to set a rule that they need at least 5 posts before their role changes.
Or if possible, adding a minimum published post for each rule change. For instance:
‘contributor’ => 100, // 5 published posts required for value change
‘author’ => 1000, // 15 published posts required for value change
‘editor’ => 10000 // 50 published posts required for value change
I do not know if such thing is possible, but it would add a great layer of possibilities for us!
Plugin Author
myCred
(@designbymerovingi)
Hey.
There is a function called mycred_count_ref_instances which does exactly this. It counts the number of times a specific reference exists in your log table.
There are two ways you can you use this function.
1. General Count
This way you count the total number of times a reference occurs in your table.
Example: Get the number of times you have given points for logins:
$logins = mycred_count_ref_instances( 'logging_in' );
2. User Specific Count
The other option is to count the occurance of a reference for a specific user.
Example: Get the number of times Joe (user id 1) have purchased a post that was set for sale:
$user_id = 1;
$purchases = mycred_count_ref_instances( 'buy_content', $user_id );