Adding Custom Reports to WP Slimstat
WP Slimstat allows you to track and analyze traffic on your WordPress site. By following these steps, you can add your own custom reports to Slimstat to better analyze the data relevant to your needs.
Note: These steps apply to Slimstat version 4. If you’re using an earlier version, we recommend upgrading to enjoy the new features in version 4.
Create Your Plugin
-
Create a folder: Under /wp-content/plugins/, create a new folder named wp-slimstat-custom-report (or any unique name).
-
Create a PHP file: Inside this folder, create a file named index.php and paste the following code:
No data to display
'; } else { // Slice the results to get only what we need $results = array_slice( $all_results, wp_slimstat_db::$filters_normalized[ 'misc' ][ 'start_from' ], wp_slimstat_db::$filters_normalized[ 'misc' ][ 'limit_results' ] ); // Paginate results, if needed wp_slimstat_reports::report_pagination( count($results), count($all_results), false ); // Loop through the resultset foreach ( $results as $a_row ) { echo "{$a_row[ 'resource' ]} {$a_row[ 'counthits' ]}
"; } } // Exit if this function was called through Ajax (refresh button) if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { die(); } } } // end of class declaration // Bootstrap if ( function_exists( 'add_action' ) ) { add_action( 'plugins_loaded', array( 'wp_slimstat_super_duper_report', 'init' ), 10 ); } **Understand the Code** - **Initialization (init): **This function checks if Slimstat is available. If it is, it sets up hooks to register the custom report and add new options. - **Register Report (add_report_info):** This function adds the custom report to Slimstat, specifying details like the report title, the function to retrieve and display data, layout classes, and the screen where the report will appear. - **Add Options (add_options):** This function adds custom settings for the report, such as toggles and text fields, ensuring they have unique keys to avoid conflicts with other settings. - **Retrieve Data (get_raw_results): **This function executes an SQL query to fetch data from the Slimstat database, grouping and counting hits for each resource. - **Display Data (raw_results_to_html):** This function formats and displays the retrieved data. It handles pagination and displays a message if no data is available. It is also designed to handle AJAX requests for data refreshes. By following these steps and understanding the code, you can create custom reports in Slimstat to better analyze your website's traffic data.