Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Created March 9, 2015 16:08
Show Gist options
  • 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;
}
@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