Back to Functions and API

Updated on Nov 05, 2023

My first Slimstat Add-on

A Slimstat Add-on is nothing more than a WordPress plugin that leverages some of the functionality implemented by Slimstat to create custom reports or tweak the behavior of the main plugin itself. Before you read this document, please make sure to familiarize yourself with the steps to write ahttps://codex.wordpress.org/Writing_a_Plugin WordPress plugin.

It’s very easy to create add-ons that extend the functionality of Slimstat. All you need to do is to hook into the right action or filter, to either add features to existing reports or to affect the behavior of the tracker. This is how you may want to structure your code:

<?php
/*
Plugin Name: WP SlimStat - My First Add-on
Description: A very cool add-on for Slimstat
Version: 1.0
*/

class wp_slimstat_my_first_add_on{
	public static function init(){
		if ( !class_exists( 'wp_slimstat' ) ) {
			return true;
		}

		// Hook into one of Slimstat's filters
		add_filter( 'slimstat_filter_pageview_stat', array( __CLASS__, 'do_something_with_this_pageview' ) );
	}

	public static function do_something_with_this_pageview( $_stat = array() ) {
		if ( $_stat[ 'ip' ] == '127.0.0.1' ) {
			$_stat[ 'other_ip' ] == '192.168.1.1';
		}

		return $_stat;
	}
}
// end of class declaration

// Bootstrap
if ( function_exists( 'add_action' ) ) {
	add_action( 'plugins_loaded', array( 'wp_slimstat_my_first_add_on', 'init' ), 15 );
}

In this example here above, we used the filter slimstat_filter_pageview_stat which receives the information about this pageview collected by the tracker right before it’s stored into the database. The function checks the IP address and performs some actions if certains conditions are met. You could use the same approach to filter pageviews based on your needs. A twin action slimstat_track_pageview allows you to call functions that perform extra computation, like send emails or edit the .htaccess file. The sky is the only a limit!