0

To encourage regular use, for logged in users, we'd like to display a line like this on the front end of the site: "Activity streak: 8 days"

We'd like it to automatically update based on site activity. i.e. in the example above the user has done something on the site for 8 days in a row. Even just a pageview would be enough.

Our users can remain logged in so this can't be measured on actual logins, it needs to be measured on some kind of activity.

I have looked at gamification plugins but they are overengineered for a simple thing like this and don't seem to offer it anyway.

1 Answer 1

1

To implement an activity streak counter in WordPress, you'll need to track user activity and update the streak counter accordingly. Below is a step-by-step guide to achieve this using custom code:

  1. Create a Custom Table: First, you'll need a custom database table to store the activity streak information.

  2. Hook into User Activity: Use WordPress hooks to detect user activity, such as page views.

  3. Update Streak Count: Implement logic to update the streak count based on the last activity date.

  4. Display the Streak: Show the activity streak on the front end.

Step 1: Create a Custom Table

Add the following code to your theme's functions.php file to create a custom table for storing user activity streaks. You can also place this in a custom plugin if you prefer.

function create_activity_streak_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'activity_streaks';
    
    $charset_collate = $wpdb->get_charset_collate();
    
    $sql = "CREATE TABLE $table_name (
        user_id bigint(20) NOT NULL,
        streak int(11) NOT NULL DEFAULT 0,
        last_activity_date date NOT NULL,
        PRIMARY KEY (user_id)
    ) $charset_collate;";
    
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

add_action('after_switch_theme', 'create_activity_streak_table');

Step 2: Hook into User Activity

Hook into a common action like template_redirect to detect page views.

function update_user_activity_streak() {
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        global $wpdb;
        $table_name = $wpdb->prefix . 'activity_streaks';
        
        $today = current_time('Y-m-d');
        $row = $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM $table_name WHERE user_id = %d", $user_id
        ));

        if ($row) {
            $last_activity_date = $row->last_activity_date;
            $streak = $row->streak;
            
            $date_diff = (strtotime($today) - strtotime($last_activity_date)) / (60 * 60 * 24);

            if ($date_diff == 1) {
                // Increment streak
                $streak++;
                $wpdb->update(
                    $table_name,
                    array('streak' => $streak, 'last_activity_date' => $today),
                    array('user_id' => $user_id)
                );
            } elseif ($date_diff > 1) {
                // Reset streak
                $wpdb->update(
                    $table_name,
                    array('streak' => 1, 'last_activity_date' => $today),
                    array('user_id' => $user_id)
                );
            } else {
                // Update last activity date without changing the streak
                $wpdb->update(
                    $table_name,
                    array('last_activity_date' => $today),
                    array('user_id' => $user_id)
                );
            }
        } else {
            // New entry for the user
            $wpdb->insert(
                $table_name,
                array(
                    'user_id' => $user_id,
                    'streak' => 1,
                    'last_activity_date' => $today
                )
            );
        }
    }
}

add_action('template_redirect', 'update_user_activity_streak');

Step 3: Display the Streak

Add a function to get the streak count for the logged-in user and display it on the front end.

function get_user_activity_streak($user_id) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'activity_streaks';
    
    $row = $wpdb->get_row($wpdb->prepare(
        "SELECT streak FROM $table_name WHERE user_id = %d", $user_id
    ));
    
    if ($row) {
        return $row->streak;
    } else {
        return 0;
    }
}

function display_activity_streak() {
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        $streak = get_user_activity_streak($user_id);
        echo '<p>Activity streak: ' . $streak . ' days</p>';
    }
}

add_action('wp_footer', 'display_activity_streak');

Summary

  1. Create a custom table to store user activity streaks.
  2. Hook into template_redirect to detect user activity and update the streak count.
  3. Display the activity streak on the front end for logged-in users.

With this implementation, the activity streak will be updated based on user activity and displayed on your WordPress site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.