1. Customize
  2. Custom Functionalities
  3. Award all your users on a specific date

Award all your users on a specific date

The following snippet shows how to award an amount of points to all users the Valentine’s day (February 14th).

function my_prefix_award_users_on_a_specific_date() {

    $points = 100;              // Amount of points to award
    $points_type = 'credits';   // Points type slug
    $date = '2018-02-14';       // Set the specific date in YYYY-MM-DD

    global $wpdb;

    $already_awarded = get_option( 'my_prefix_user_awarded_on_' . $date );

    if( ! $already_awarded ) {

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

        foreach( $users as $user ) {
            // Award the points to the user
            gamipress_award_points_to_user( $user->ID, $points, $points_type );
        }

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

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

Yearly or monthly award

You can change the date check into a more dynamic way, like award this all years or all months using the PHP date() function like in the next example:

// Make date matches the February 14th of all years
$date = date('Y') . '-02-14';
// Make date matches the 14th of all months of all years
$date = date('Y-m') . '-14';

Award users by role

This snippet will award to all users without exception. You can award users by role by replacing the part:

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

by:

$role = 'subscriber'; // Set the role you want to filter

// Get all users by role
$users = $wpdb->get_results( "SELECT ID
FROM {$wpdb->users}
INNER JOIN {$wpdb->usermeta} ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )
WHERE {$wpdb->usermeta}.meta_key = 'wp_capabilities'
AND {$wpdb->usermeta}.meta_value LIKE '%{$role}%'" );

For more information about roles, check the Roles and Capabilities on WordPress official docs.

Additional Notes

On the above snippet users are awarded with points, but if you want to award an achievement, you can use the function gamipress_award_achievement_to_user( $achievement_id, $user_id ).

Also, If you want to award a rank, you can use the function gamipress_award_rank_to_user( $rank_id, $user_id ).

Was this article helpful to you? Yes No

How can we help?