1. Customize
  2. Custom Functionalities
  3. Reset users points yearly

Reset users points yearly

The following snippet shows how to reset points of a specific points type to all users yearly.

function my_prefix_reset_users_points_yearly() {

    $points_type = 'credits';   // Points type slug to be reset
    $date = '01-01';            // Set the date in MM-DD

    global $wpdb;

    // Prepend the current year to the date to get a format like YYYY-MM-DD
    $date = date('Y') . '-' . $date;
    $user_meta = GamiPress()->db->usermeta;

    $already_reset = get_option( 'my_prefix_user_reset_on_' . $date );

    if( ! $already_reset ) {

        // Delete all balance metas of this points type
        $wpdb->query( "DELETE um FROM {$user_meta} AS um WHERE um.meta_key = '_gamipress_{$points_type}_points'" );

        update_option( 'my_prefix_user_reset_on_' . $date, 1 );
    }

}
add_action( 'init', 'my_prefix_reset_users_points_yearly' );

Additional Notes

In addition you can change the date check into a more dynamic way, like reset points monthly or daily using the PHP date() function like in the next example:

// Make date matches the 1st of all months of all years (to reset monthly)
$date = date('Y-m') . '-01';
// Make date matches all days (to reset daily)
$date = date('Y-m-d');

How can we help?