Back to Functions and API

Updated on Jun 09, 2024

Retrieving and Processing Data from the Database with PHP

Slimstat’s shortcodes are a handy way to display various statistics on your website. However, sometimes you need more flexibility and control over data processing. That’s where Slimstat’s built-in API functions come in, offering advanced capabilities and customization options.

Why Use API Functions Instead of Shortcodes?

Shortcodes are great for simple use cases but have limitations. You might need to:

  • Combine data from multiple sources
  • Apply complex filters
  • Perform custom calculations

These advanced tasks can be difficult to achieve with shortcodes alone.

What Can API Functions Do?

Slimstat’s API functions let you directly access the plugin’s core functionality from your PHP code. You can:

  • Retrieve raw data
  • Apply detailed filters
  • Manipulate results to fit your needs

This lets you create customized reports, visualizations, and integrate Slimstat’s data into other applications or processes.

Example Use Case: Real-Time Pageviews

Imagine you want to build a real-time analytics dashboard for your WordPress site to monitor traffic spikes or issues. You can use both shortcodes and API functions for this.

Retrieve Real-Time Pageviews with a Shortcode

To get the number of pageviews recorded in the last 5 minutes, use this shortcode:

[slimstat f='count' w='id']strtotime equals now&&&interval_direction equals 1&&&interval_minutes equals 5[/slimstat]

Embed this into your site to display the real-time pageview count.

Enhance Flexibility with API Functions

For more complex needs, like retrieving pageviews for a specific date range, use API functions. Here’s how to get pageviews for the first 5 days of February 2017:

1. Include the API File

include_once( WP_PLUGIN_DIR . '/wp-slimstat/admin/view/wp-slimstat-db.php' );

2. Set the Filters

wp_slimstat_db::init( 'strtotime equals now&&&interval_minutes equals 5' );

3. Call the API Function

echo wp_slimstat_db::count_records( 'ip', '', true );

Using the API functions, you can retrieve and process data to create custom reports or integrate the data into other tools.

Real-Time Updates and Visualization

With the data retrieved, you can update your dashboard in real-time using JavaScript to periodically fetch and display the latest pageview count. You can also include other metrics like unique visitors, top referrers, or geographic data, all retrieved using Slimstat’s API functions.


By using Slimstat’s API functions, you gain the ability to:

  • Combine data from various sources
  • Apply complex filtering criteria
  • Create customized reports and visualizations

This approach offers the flexibility to meet your specific data processing needs.