Functions and API

Retrieving and Processing Data from the Database with PHP

Applies to SlimStat 6.0.0 · checked

Use the SlimStat database API when you need to process a report in PHP. Run this inside WordPress after plugins load; SlimStat is not a standalone PHP library.

Count retained pageviews for administrators

if ( current_user_can( 'manage_options' ) && class_exists( 'wp_slimstat' ) ) {
    require_once WP_PLUGIN_DIR . '/wp-slimstat/admin/view/wp-slimstat-db.php';
    wp_slimstat_db::init( '', false );
    $pageviews = wp_slimstat_db::count_records( 'id', '', false );
    echo esc_html( number_format_i18n( (int) $pageviews ) );
}

This example counts pageviews in the live table across all dates (not archived or deleted records), with date filtering disabled. Use id instead of ip to include pageviews that have no stored IP. The capability check limits this example to administrators; use your application’s explicit access policy for other audiences.

Filter before querying

Pass trusted filter expressions to wp_slimstat_db::init(), then call count_records(), get_recent() or get_top(). Validate user input before building a filter, and never pass request input as raw SQL. Initializing filters affects shared report state.

For a public aggregate count, the native shortcode may be enough:

[slimstat f="count" w="id"]

A cached page can show an older count. Do not put visitor identifiers on a public page. Read the API method reference and shortcode examples before building a custom report.