Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Created August 15, 2016 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/8c03987b961d85c1ddbee8654b520ed0 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/8c03987b961d85c1ddbee8654b520ed0 to your computer and use it in GitHub Desktop.
Prevent users from reaching a negative myCRED point balance. Will decline all point transactions that will result in a users balance going minus.
/**
* Prevent Negative Balances
* Make sure users can never get a negative balance. Will allow users
* to gain a zero balance.
* @since 1.0
* @version 1.0
*/
add_filter( 'mycred_add', 'mycred_pro_no_negative_balance', 999, 2 );
function mycred_pro_no_negative_balance( $reply, $request ) {
// Ignore if allready declined
if ( $reply === false ) return $reply;
extract( $request );
// Ignore point gains
if ( $amount > 0 ) return $reply;
// Get uses balance
$balance = $mycred->get_users_balance( $user_id, $type );
// Prevent negative balances
if ( $balance <= 0 || ( $balance - $amount ) < 0 ) return false;
// all else return default
return $reply;
}
@seollc
Copy link

seollc commented Nov 1, 2016

Where do I add the code to prevent users from getting to a negative balance without earning or buying more points?

@dynamitemedia
Copy link

im guessing this goes in the the themes functions?

@BuddyBPop007
Copy link

Where do I add the code to prevent users from getting to a negative balance without earning or buying more points?

This would be exactly what I'm looking for as well as it still allows for users to complete the action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment