Block users with negative balances

Home / Tutorials / Block users with negative balances


  1. Description
  2. Requirements
  3. Authenticate
  4. Done


In this tutorial, I will show you how you can block users from logging in if their account balance goes into minus.

For this tutorial, we will assume that our myCred installation has hooks which deduct points from users, creating the possibility of user’s accounts going minus.


  • Hooks that deduct points from users creating the possibility for users accounts to go minus.


For this feature we will need to hook into WordPress using the authenticate filter. This filter fires when the user has entered their username and password and clicked “Login”. Our filter will check the users point balance and if it is below zero, we will return an error message which will prevent the user from logging in. Place the following code snippet into your theme’s function.php file.

 * Block Negative Balances
 * Stop users from logging into your website if they have a negative
 * myCRED balance.
 * @version 1.0
add_filter( 'authenticate', 'mycred_block_negative_balances', 990, 3 );
function mycred_block_negative_balances( $user, $username, $password ) {

 	// Make sure myCred is installed
 	if ( ! defined( 'myCRED_VERSION' ) ) return $user;

 	// The point type we want to use
 	$type = 'mycred_default';

 	// Load myCRED
 	$mycred = mycred( $type );
 	$account = get_user_by( 'login', $username );

 	if ( isset( $account->ID ) ) {

 		if ( ! $mycred->exclude_user( $account->ID ) ) {

 		if ( $mycred->get_users_balance( $account->ID, $type ) < 0 )
 			return new WP_Error( 'mycred_negative', __( 'Account Closed' ) );



 	return $user;



There you have it. It’s as simple as that. You can of course enter your own message to show users and as the commented in the code, you can change it to block users with zero points as well.

  1. Hi,
    This great tutorial shows how to block users from logging in if their account balance goes into minus.

    Would it be a way for users to «click» buy now button to «add points» to their balance directly beside the message: Account Closed or under login logo ?

    1. Since we are blocking the user from logging in, we will not have a user id which is required for the buyCRED add-on to generate a buy now button. I will however look into this as I feel it is a really cool idea and see how best to do this.

    2. I would too like to find a way for them to buy points if account is blocked to restore access. Any updates on this would be great!

    3. Since this tutorial blocks users from logging in to your website if they do not have points, you can not get them to buy more points (since they must be logged in to do so). You might want to look at a different solution all together, where you might allow users to login to the site with zero points but then force them to a page where they can buy points.

  2. Thank you for your Reply,
    I really like this feature, so maybe the best way for now, would a simple message to users saying «Please email siteadmin/webmaster» in order to buy points.

    * Transaction woud be external of course and site admin would need to adjust points directly from user profile account.

    Thank you for this great plugin – Regards

  3. Hi Gabriel,

    Great plugin! Out of the box this has pretty much everything I was looking for! One question I did have though (sort of related to this topic) was if there was a way to prevent users from having negative points. I want to set the minimum at 0 points or something like that, so when a user tries to perform a task that has a hook which deducts points, it won’t let them do it if they don’t have sufficient points. Essentially 0 would be the lowest amount of points, and if a user needs more to perform the task, they would need to purchase more.

    Thanks for the help!

    1. You would need to hook into the mycred_add function and before awarding points make sure the balance is not zero or if deducting points, the amount being deducted does not take a user below zero.


      add_filter( 'mycred_add', 'restrict_negative_balances', 1, 3 );
      function restrict_negative_balances( $reply, $request, $mycred ) {
      	if ( $reply === false ) return $reply;
      	// The amount the user will gain / loose
      	$amount = $mycred->number( $request['amount'] );
      	// Users current balance
      	$current_balance = $mycred->get_users_cred( $request['user_id'] );
      	// If balance is zero - decline
      	if ( $amount < $mycred->zero() && $current_balance <= $mycred->zero() ) return false;
      	// If we are deducting points, make sure the amount will not take us below zero
      	if ( $amount < $mycred->zero() && $current_balance-$amount < $mycred->zero() ) return false;
      	return $reply;
  4. Hi. I’m new to your plugin but I can really see the potential. One thing I”m trying to work out that would be very beneficial:

    I want to use the points system as a reward system that allows users to spend them on guest posts. I set the reward for posts as negative hoping that any insufficient balance would dissalow the user from submitting the post, but instead the mycred balance just went negative.

    Is there any way to have the plugin check whether there are enough points to complete the action and if not, then dissalow it?

    As another thought for the future of the plugin, if there was some way to develop posting credits as a secondary type of currency. For example, 100 mycred points are worth 1 post credit, and the user can spend the credit to submit a guest post. I think that would be an in-demand feature and one I’d be willing to pay for.

    Thanks much,

  5. Actually I was wondering if this could even be accomplished in an easier way. I was reading through your mycred ranks tutorial. If it is possible to change the wordpress user privleges based on their mycred rank then I could achieve this with no code customization necessary.

    If you don’t have enough points, you won’t have the rank required to post articles. Once you post and lose -100 points, you may drop in rank and have to earn it back. It was not clear from the tutorial however, whether the rank system can change user privileges or if it is just a badge system.

    Thanks again!

    1. Hey.

      myCRED has no built-in capability adjustments for WordPress. Initially I was thinking of connecting things with roles but then I realized there are a lot of different approaches for how user capabilities are handled in WordPress sites and building something that would accomodate / work with all of these would be a headache.

      But that does not mean it is not impossible. In 1.3.2 I included two custom actions in myCRED: mycred_user_got_demoted and mycred_user_got_promoted. These fire when a users rank changes. Demoted is triggered when the new rank the user gained has a lower minimum point requirement then their current rank while promoted is triggered for the complete opposite.

      You could use these hooks to execute custom code like changing a users role in WP. Just make sure the user in question is not an admin.

  6. Hi, your plugin interests me a lot, and I’ve already tried it. My problem is that I want to redirect users to a points purchase page in case its balance is negative. Can you help me?

    1. Hi Gabriel, thank you for the answer. I tried but it does not work, is the $ balance variable the current balance or the total balance of the user? Because what I want to do is when the current balance is below 0 then it will be redirected to another page

    2. Hi Gabriel, thank you for the answer. I tried but it does not work, the user’s current balance is always below 0 and does not redirect to the desired page, is the $ balance variable the current balance or the total of Balance of the user? Because what I want to do is when the current balance is below 0 then it will be redirected to another page

Leave a Reply

Last edited June 11, 2021