• Resolved iamonlythird

    (@iamonlythird)


    I made this request on the CubePoints forums back when it was live and Justin, one of the developers (alongside many others) discussed about the possibilities of this and that they were working on it. Unfortunately, CubePoints threw the towel for a while ago so I thought that I would bring this feature request here.

    Is it possible to change a user’s role once they reach a certain amount of points (or rank)? This will allow a system where users can gain new capabilities when “leveling” up. I am sure that you can appreciate that this feature will bring a completely new wave of opportunities for developers to create extensive and rich platforms (without the maintenance of manually updating the users roles…).

    I hope that you will consider such feature for this amazing plugin!

    http://ww.wp.xz.cn/plugins/mycred/

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author myCred

    (@designbymerovingi)

    Are we talking the default WordPress roles or roles customized though a role management plugin?

    Thread Starter iamonlythird

    (@iamonlythird)

    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.

    Thread Starter iamonlythird

    (@iamonlythird)

    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.

    Thread Starter iamonlythird

    (@iamonlythird)

    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.

    Thread Starter iamonlythird

    (@iamonlythird)

    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.

    Thread Starter iamonlythird

    (@iamonlythird)

    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.

    Thread Starter iamonlythird

    (@iamonlythird)

    I will try this code out and report back in the coming days. Millions of thanks Gabriel.

    Thread Starter iamonlythird

    (@iamonlythird)

    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.

    Thread Starter iamonlythird

    (@iamonlythird)

    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 );
Viewing 15 replies - 1 through 15 (of 17 total)

The topic ‘Feature request: Points/Rank affecting user roles’ is closed to new replies.