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;

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

    if( ! $already_reset ) {

        // Get all users stored
        $users = $wpdb->get_results( "SELECT ID FROM {$wpdb->users}" );

        foreach( $users as $user ) {

            // Get user's points
            $user_points = gamipress_get_user_points( $user->ID, $points_type );

            // Deduct same amount of points that current user's amount
            gamipress_deduct_points_to_user( $user->ID, $user_points, $points_type );
        }

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

}
add_action( 'init', 'my_prefix_reset_users_points_yearly' );
Was this article helpful to you? Yes No

How can we help?