Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active January 28, 2021 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/15cf6d6a8898dc68a39340d3d6965a10 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/15cf6d6a8898dc68a39340d3d6965a10 to your computer and use it in GitHub Desktop.
Example where we enforce a maximum balance of 1000 points in myCRED. If a user is about to get points that exceeds this limit, we decline the transaction. Goes into your custom plugin or child theme's functions.php file.
/**
* Enforce Maximum
* Make sure users can not earn more than 1000 points.
* @version 1.0
*/
function mycred_enforce_max_balance( $run, $request, $mycred ) {
extract( $request );
// The maximum balance a user can have
$maximum = 1000;
// Get the users balance
$balance = $mycred->get_users_balance( $user_id );
// Check if we already reached our max
if ( $balance >= $maximum ) return false;
// Next make sure this transaction will not bring us over the limit
$balance += $amount;
if ( $balance > $maximum ) return false;
// Below limit, please continue
return $run;
}
add_filter( 'mycred_add', 'mycred_enforce_max_balance', 1, 3 );
@rpetitto
Copy link

How can I specify a particular point type? I have 3 point types and only one of them should be capped. Thanks!

@rpetitto
Copy link

Secondly, how can I set the balance to the maximum if the mycred_add filters comes back false? Likewise, similar to the prevent negative balance gist you created, how can I set the balance to 0 regardless of the amount value requested? Thanks!

@princejohnokosun
Copy link

How can I set this post type and also how to can make it the max when a transaction higher is made.

Please help

@josephtersoo
Copy link

@gabrielmerovingi i need help.
How can i restrict how many points a post or page can give out.
example: i create a post or page i want that post or page to award x number of points only or award x numbers or users on points for that particular post or page. once the target is meet, subsequent visit will will received a not receive points again for that post.

or

using the click on links.
a link can only award specific number of points using shortcode.
e.g i create a link, that link should give out only 500 points, once that link has given out 500 points, it shouldn't give out points again for subsequent clicks.

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