Back to Functions and API

Updated on Jun 09, 2024

How to Get the Usernames of Visitors to a Specific Page

Slimstat provides two main methods to check who visited a specific page on your website: shortcodes and direct API calls.

Option 1: Via Shortcode

You can display the usernames of visitors who accessed a specific page by inserting this shortcode into the content area of that page or post:

[slimstat f='recent' w='username']content_id equals current[/slimstat]

This shortcode will show the usernames of visitors to that page within the current month. For more precise time ranges (e.g., today only, this year), check out the Slimstat shortcode examples.

Option 2: Via Direct API / PHP calls

Alternatively, you can directly access the data via PHP by adding this code to your single.php template file:

$current_post_id = get_the_ID();
include_once( WP_PLUGIN_DIR . '/wp-slimstat/admin/view/wp-slimstat-db.php' );
wp_slimstat_db::init( 'content_id equals ' . $current_post_id );
$array_of_users = wp_slimstat_db::get_recent( 'username' );

// Now loop through the array to display your data

This code retrieves an array of the usernames that visited the current page recently. You can then loop through that array to display the usernames wherever you need them. Refer to the Slimstat documentation for further details on manipulating and outputting data from the database in PHP.

Why Get Visitor Usernames?

Knowing which users visited a specific page can be very useful for improving user engagement. For example, if you publish a popular new blog post, you can follow up with the visitors in a more personalized way based on their usernames. Or you could run a promotion targeting those visitors.