• Resolved Chris

    (@kryap)


    Hi,

    Great plugin!
    I was wondering if it’s possible to limit myCred to certain user roles?
    Frankly it doesnt matter if all users get points, but I do want to limit the ranking to certain roles only.

    Is this possible, and how?

    Thanks,
    Chris

    https://wordpress.org/plugins/mycred/

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

    (@designbymerovingi)

    Hi.

    Yes you can exclude based on roles but it would require a small code snippet.
    Here is an example where we exclude subscribers, contributors and authors:

    add_filter( 'mycred_exclude_user', 'mycred_pro_exclude_by_role', 10, 2 );
    function mycred_pro_exclude_by_role( $exclude, $user_id ) {
    
    	// Exclude subscribers
    	if ( ! user_can( $user_id, 'delete_posts' ) ) return true;
    
    	// Exclude contributors
    	if ( ! user_can( $user_id, 'publish_posts' ) ) return true;
    
    	// Exclude authors
    	if ( ! user_can( $user_id, 'read_private_pages' ) ) return true;
    
    	return $exclude;
    
    }

    You can adjust this code and just exclude those roles you need and not all of these. The WordPress codex has all default capabilities documented but you might want to change which capability to check for if you use custom roles.

    The above code goes into your child theme’s functions.php file and will only effect myCRED.

    Once this code is used, all users who do not have a specific rank will be excluded from using myCRED. This means they will no longer have a balance, use any shortcode or widgets etc. Now, if your users have already started gaining points, then you would need to first set their balance to zero before adding this code otherwise they will still show up in leaderboards.

    To exclude users with zero balances from the leaderboard, you can set the exclude_zero=”1″ shortcode attribute.

    Thread Starter Chris

    (@kryap)

    Thanks for the fast and extensive reply Gabriel.
    If I understand correctly you can exclude users based on their capabilities. But is it also possible to simply use the (custom) role name?
    I noticed you can exclude users from your list manually which works for us. Only the admin user cannot be excluded.

    Thanks,
    Chris

    Plugin Author myCred

    (@designbymerovingi)

    You can use that filter to exclude based on anything really. If you prefer to check a users role instead of a capability then that would work as well.

    Yes you can manually exclude users as an admin and you can, if needed, exclude admins and editors from myCRED on the settings page.

    I added in that filter in case that was not enough and we needed to exclude based on some other detail.

    Hi Gabriel,
    First, thanks for the great plugin.

    I wander if I can just use current_user_can() function instead of user_can(). That way WP will take care of the user id on its own.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Limit to user role’ is closed to new replies.