Back to Functions and API

Updated on Jun 09, 2024

Creating Your First SlimStat Add-on

SlimStat is a powerful WordPress analytics plugin that provides valuable insights into your website’s traffic and visitor behavior. While SlimStat comes packed with a wide range of features, you may sometimes need additional functionality tailored to your specific requirements. This is where SlimStat Add-ons come into play.

What are SlimStat Add-ons?

SlimStat Add-ons are WordPress plugins that extend the capabilities of SlimStat. They allow you to create custom reports, modify the behavior of the main plugin, or add new features according to your needs. Whether you’re an experienced developer or just starting out, creating add-ons for SlimStat is a straightforward process.

Prerequisites

Before you start developing a SlimStat Add-on, it’s essential to have a basic understanding of WordPress plugin development. The WordPress Plugin Handbook is an excellent resource that provides comprehensive guidance on plugin structure, best practices, and other essential topics.

Creating Your Add-on

To create your first SlimStat Add-on, follow these steps:

  1. Create a new folder for your plugin within the wp-content/plugins/ directory of your WordPress installation.
  2. Inside this folder, create a new PHP file (e.g., my-first-slimstat-addon.php).
  3. Add the following code to the PHP file:
<?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 );
}

This code sets up the basic structure for your SlimStat Add-on. The init() method checks if the main SlimStat plugin is installed and active, and then hooks into the slimstat_filter_pageview_stat filter. This filter allows you to modify the pageview data before it is stored by SlimStat.

The modify_pageview_data() method is where you can implement your custom logic for modifying the pageview data. In this example, it simply returns the $stat array without any modifications, but you can add your own code here to manipulate the data as needed.

Practical Example: Custom IP Handling

Let’s consider a practical example of how you can use a SlimStat Add-on to modify the IP address recorded for visitors accessing your site from localhost (127.0.0.1). In this scenario, you might want to replace the localhost IP with a different IP address for better tracking or testing purposes.

Update the modify_pageview_data() method in your Add-on with the following code:

public static function modify_pageview_data($stat = array()) {
    if ($stat['ip'] === '127.0.0.1') {
        $stat['ip'] = '192.168.1.1'; // Replace with your desired IP address
    }

    return $stat;
}

This code checks if the visitor’s IP address is 127.0.0.1 (localhost). If it is, it replaces the IP address with 192.168.1.1 (or any other IP address you prefer) before returning the modified $stat array.

With this Add-on active, SlimStat will now record the specified IP address instead of 127.0.0.1 for visitors accessing your site from localhost.

Conclusion

Creating a SlimStat Add-on is a powerful way to extend the functionality of SlimStat and tailor it to your specific needs. By following the steps outlined in this guide, you can develop your own add-ons and unlock new possibilities for your WordPress analytics workflow.

Remember, this is just a basic example, and you can implement more complex logic within your Add-on to achieve various customizations and enhancements for SlimStat.