Skip to content

Instantly share code, notes, and snippets.

@askaryabbas
Created February 16, 2017 19:32
Show Gist options
  • Save askaryabbas/6b4155d63008bdf7fae9f3597ad99ad0 to your computer and use it in GitHub Desktop.
Save askaryabbas/6b4155d63008bdf7fae9f3597ad99ad0 to your computer and use it in GitHub Desktop.
/**
* MyCred Monthly Payouts
* On the first page load on the first day of each month
* award 10 points to all users with the role "Subscriber".
* @version 1.0
*/
add_action( 'mycred_init', 'mycred_pro_monthly_payouts' );
function mycred_pro_monthly_payouts() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return;
$this_month = date( 'n' );
if ( get_option( 'mycred_monthly_payout', 0 ) != $this_month ) {
// Grab all users for the set role
$users = get_users( array(
'role' => 'subscriber', // The role
'fields' => array( 'ID' )
) );
// If users were found
if ( $users ) {
$type = 'mycred_default';
$mycred = mycred( $type );
// Loop though users
foreach ( $users as $user ) {
// Make sure user is not excluded
if ( $mycred->exclude_user( $user->ID ) ) continue;
// Make sure users only get this once per month
if ( $mycred->has_entry( 'monthly_payout', $this_month, $user->ID, '', $type ) ) continue;
// Payout
$mycred->add_creds(
'monthly_payout',
$user->ID,
10,
'Monthly %_plural% payout',
$this_month,
'',
$type
);
}
update_option( 'mycred_monthly_payout', $this_month );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment