Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Created March 9, 2015 16:08
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/ab9b6c1fa2fb46d05d25 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/ab9b6c1fa2fb46d05d25 to your computer and use it in GitHub Desktop.
Send emails to your users when they reach a certain amount of points or "milestones". This code example sends emails at three milestones: 100 points, 200 points and 300 points. Paste into your child theme's functions.php file.
/**
* Add Custom Email Instances
* First we add in our custom instances so we can select them when we create
* a new email notice.
* @version 1.0
*/
add_filter( 'mycred_email_instances', 'mycred_pro_add_milestone_email_instances' );
function mycred_pro_add_milestone_email_instances( $instances ) {
$instances['accountnum'] = array(
'label' => 'Point Milestones',
'100' => 'Reached 100 points',
'200' => 'Reached 200 points',
'300' => 'Reached 300 points',
'end' => ''
);
return $instances;
}
/**
* Detect Instnaces
* Next we will need to "detect" when a user reaches each milestone. To prevent
* them from getting emails each time they gain points over a milestone we save
* a marker.
* @version 1.0
*/
add_filter( 'mycred_get_email_events', 'mycred_pro_detect_custom_email_instances', 10, 3 );
function mycred_pro_detect_custom_email_instances( $events, $request, $mycred ) {
extract( $request );
$users_balance = $mycred->get_users_balance( $user_id, $type );
if ( $users_balance > 100 && get_user_meta( $user_id, 'got_for_hundred', true ) == '' ) {
$events[] = 'accountnum|100';
add_user_meta( $user_id, 'got_for_hundred', time() );
}
if ( $users_balance > 200 && get_user_meta( $user_id, 'got_for_twohundred', true ) == '' ) {
$events[] = 'accountnum|200';
add_user_meta( $user_id, 'got_for_twohundred', time() );
}
if ( $users_balance > 300 && get_user_meta( $user_id, 'got_for_threehundred', true ) == '' ) {
$events[] = 'accountnum|300';
add_user_meta( $user_id, 'got_for_threehundred', time() );
}
return $events;
}
@dynamitemedia
Copy link

dynamitemedia commented Apr 10, 2017

im getting this error using the latest mycred

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'mycredpro_add_reset_balance_option' not found or invalid function name in /home/burstexc/public_html/wp-includes/class-wp-hook.php on line 298

i added this to my themes function.php

@welshlamb10
Copy link

@gabrielmerovingi

This code seems great. Although in the listing its shows the word Array rather than the specified label 'Point Milestone'?
Is it possible to have this so that different emails can be sent based on the milestones?

e.g. 10 Points - email specifically for 10 points
20 points - email specifically for 20 points

Thanks

@gabrielmerovingi
Copy link
Author

gabrielmerovingi commented Sep 28, 2021

@welshlamb10

Unfortunately I am unsure this still works with the current version of myCRED as I am no longer involved with the plugin since 2018. The code was written for older version.

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