Below, you’ll find a detailed guide on how to add the Slimstat Analytics Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Slimstat Analytics Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Slimstat Analytics Plugin and the shortcodes it provides:
"Slimstat Analytics is a powerful WordPress plugin that delivers real-time access to your site's visitor data. With WP-Slimstat, gain insightful metrics to enhance user engagement and site performance."
- [slimstat]
Slimstat Analytics [slimstat] Shortcode
The WP-Slimstat shortcode is a tool that allows users to display specific statistical data on their WordPress site. It uses various attributes like ‘f’, ‘w’, ‘s’, ‘o’ to customize the output. The ‘f’ attribute defines the function, ‘w’ specifies the column or widget to use, ‘s’ sets the separator, and ‘o’ is the offset for counters. It validates parameters, loads localization files, and handles errors, ensuring accurate data presentation.
Shortcode: [slimstat]
Parameters
Here is a list of all possible slimstat shortcode parameters and attributes:
f
– Specifies the function to be used, such as ‘recent’, ‘popular’, ‘count’, or ‘widget’w
– Specifies the column to be used for ‘recent’, ‘popular’ and ‘count’ or the widget to be useds
– Specifies the separator to be used between elementso
– Specifies the offset for counters
Examples and Usage
Basic example – Displaying the most recent posts using the slimstat shortcode.
[slimstat f='recent' w='post_link' /]
Advanced examples
Using the shortcode to display the count of records for a specific column. In this example, we are counting the records for the ‘username’ column.
[slimstat f='count' w='username' /]
Displaying a specific widget using the shortcode. In this case, we’re displaying the widget with the ID ‘slim_p1_01’.
[slimstat f='widget' w='slim_p1_01' /]
Displaying the top search terms used on your website. The ‘top’ function is used to retrieve the most popular entries for a specific column.
[slimstat f='top' w='searchterms' /]
Using the shortcode to display recent posts, but separating each post with a custom separator. In this case, a pipe ‘|’ symbol is used as the separator.
[slimstat f='recent' w='post_link' s='|' /]
PHP Function Code
In case you have difficulties debugging what causing issues with [slimstat]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('slimstat', array(__CLASS__, 'slimstat_shortcode'), 15);
Shortcode PHP function:
function slimstat_shortcode($_attributes = '', $_content = '')
{
shortcode_atts(array(
'f' => '', // recent, popular, count, widget
'w' => '', // column to use (for recent, popular and count) or widget to use
's' => ' ', // separator
'o' => 0 // offset for counters
), $_attributes);
$f = isset($_attributes['f']) ? $_attributes['f'] : '';
$w = isset($_attributes['w']) ? $_attributes['w'] : '';
$s = isset($_attributes['s']) ? $_attributes['s'] : '';
$o = isset($_attributes['o']) ? $_attributes['o'] : 0;
$output = $where = $as_column = '';
$s = "<span class='slimstat-item-separator'>$s</span>";
// Load the localization files (for languages, operating systems, etc)
load_plugin_textdomain('wp-slimstat', false, '/wp-slimstat/languages');
// Look for required fields
if (empty($f) || empty($w)) {
return '<!-- Slimstat Shortcode Error: missing parameter -->';
}
// Validation the parameter w
if (in_array($w, array('count', 'display_name', 'hostname', 'post_link', 'post_link_no_qs', 'dt', 'username', 'post_link', 'ip', 'id', 'searchterms', 'username', 'resource', 'slim_p1_01', 'slim_p1_03', 'slim_p1_04', 'slim_p1_06', 'slim_p1_08', 'slim_p1_10', 'slim_p1_11', 'slim_p1_12', 'slim_p1_13', 'slim_p1_15', 'slim_p1_17', 'slim_p1_18', 'slim_p1_19_01', 'slim_p2_01', 'slim_p2_02', 'slim_p2_03', 'slim_p2_04', 'slim_p2_05', 'slim_p2_06', 'slim_p2_07', 'slim_p2_08', 'slim_p2_09', 'slim_p2_12', 'slim_p2_13', 'slim_p2_14', 'slim_p2_15', 'slim_p2_16', 'slim_p2_17', 'slim_p2_18', 'slim_p2_19', 'slim_p2_20', 'slim_p2_21', 'slim_p2_22_01', 'slim_p2_24', 'slim_p2_25', 'slim_p3_01', 'slim_p3_02', 'slim_p4_01', 'slim_p4_02', 'slim_p4_04', 'slim_p4_05', 'slim_p4_06', 'slim_p4_07', 'slim_p4_09', 'slim_p4_10', 'slim_p4_11', 'slim_p4_12', 'slim_p4_13', 'slim_p4_15', 'slim_p4_16', 'slim_p4_18', 'slim_p4_19', 'slim_p4_20', 'slim_p4_21', 'slim_p4_22', 'slim_p4_23', 'slim_p4_24', 'slim_p4_25', 'slim_p4_26_01', 'slim_p4_27', 'slim_p6_01')) == false) {
return '<!-- Slimstat Shortcode Error: invalid parameter for w -->';
}
// Include the Reports Library, but don't initialize the database, since we will do that separately later
include_once(plugin_dir_path(__FILE__) . 'admin/view/wp-slimstat-reports.php');
wp_slimstat_reports::init();
/**
* @SecurityProfile https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2023-0630
* Disabled because of the report from WP Scan
*/
// Init the database library with the appropriate filters
/*if ( strpos ( $_content, 'WHERE:' ) !== false ) {
$where = html_entity_decode( str_replace( 'WHERE:', '', $_content ), ENT_QUOTES, 'UTF-8' );
}
else{*/
wp_slimstat_db::init(html_entity_decode($_content, ENT_QUOTES, 'UTF-8'));
//}
switch ($f) {
case 'count':
case 'count-all':
$output = wp_slimstat_db::count_records($w, $where, strpos($f, 'all') === false) + $o;
break;
case 'widget':
if (empty(wp_slimstat_reports::$reports[$w])) {
return __('Invalid Report ID', 'wp-slimstat');
}
wp_register_style('wp-slimstat-frontend', plugins_url('/admin/assets/css/slimstat.css', __FILE__));
wp_enqueue_style('wp-slimstat-frontend');
wp_slimstat_reports::$reports[$w]['callback_args']['is_widget'] = true;
ob_start();
echo wp_slimstat_reports::report_header($w);
call_user_func(wp_slimstat_reports::$reports[$w]['callback'], wp_slimstat_reports::$reports[$w]['callback_args']);
wp_slimstat_reports::report_footer();
$output = ob_get_contents();
ob_end_clean();
break;
case 'recent':
case 'recent-all':
case 'top':
case 'top-all':
$function = 'get_' . str_replace('-all', '', $f);
if ($w == '*') {
$w = 'id';
}
$w = esc_html($w);
$w = self::string_to_array($w);
// Some columns are 'special' and need be removed from the list
$w_clean = array_diff($w, array('count', 'display_name', 'hostname', 'post_link', 'post_link_no_qs', 'dt'));
// The special value 'display_name' requires the username to be retrieved
if (in_array('display_name', $w)) {
$w_clean[] = 'username';
}
// The special value 'post_list' requires the resource to be retrieved
if (in_array('post_link', $w)) {
$w_clean[] = 'resource';
}
// The special value 'post_list_no_qs' requires a substring to be calculated
if (in_array('post_link_no_qs', $w)) {
$w_clean = array('SUBSTRING_INDEX( resource, "' . (!get_option('permalink_structure') ? '&' : '?') . '", 1 )');
$as_column = 'resource';
}
// Retrieve the data
$results = wp_slimstat_db::$function(implode(', ', $w_clean), $where, '', strpos($f, 'all') === false, $as_column);
// No data? No problem!
if (empty($results)) {
return '<!-- Slimstat Shortcode: No Data -->';
}
// Are nice permalinks enabled?
$permalinks_enabled = get_option('permalink_structure');
// Format results
$output = array();
foreach ($results as $result_idx => $a_result) {
foreach ($w as $a_column) {
$output[$result_idx][$a_column] = "<span class='col-$a_column'>";
switch ($a_column) {
case 'count':
$output[$result_idx][$a_column] .= $a_result['counthits'];
break;
case 'country':
$output[$result_idx][$a_column] .= wp_slimstat_i18n::get_string('c-' . $a_result[$a_column]);
break;
case 'display_name':
$user_details = get_user_by('login', $a_result['username']);
if (!empty($user_details)) {
$output[$result_idx][$a_column] .= $user_details->display_name;
} else {
$output[$result_idx][$a_column] .= $a_result['username'];
}
break;
case 'dt':
$output[$result_idx][$a_column] .= date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $a_result['dt']);
break;
case 'hostname':
$output[$result_idx][$a_column] .= self::gethostbyaddr($a_result['ip']);
break;
case 'language':
$output[$result_idx][$a_column] .= wp_slimstat_i18n::get_string('l-' . $a_result[$a_column]);
break;
case 'platform':
$output[$result_idx][$a_column] .= wp_slimstat_i18n::get_string($a_result[$a_column]);
break;
case 'post_link':
case 'post_link_no_qs':
$post_id = url_to_postid($a_result['resource']);
if ($post_id > 0) {
$output[$result_idx][$a_column] .= "<a href='{$a_result[ 'resource' ]}'>" . get_the_title($post_id) . '</a>';
} else {
$output[$result_idx][$a_column] .= "<a href='{$a_result[ 'resource' ]}'>{$a_result[ 'resource' ]}</a>";
}
break;
default:
$output[$result_idx][$a_column] .= isset($a_result[$a_column]) ? $a_result[$a_column] : '';
break;
}
$output[$result_idx][$a_column] .= '</span>';
}
$output[$result_idx] = '<li>' . implode($s, $output[$result_idx]) . '</li>';
}
$output = '<ul class="slimstat-shortcode ' . $f . implode('-', $w) . '">' . implode('', $output) . '</ul>';
break;
default:
break;
}
return $output;
}
Code file location:
wp-slimstat/wp-slimstat/wp-slimstat.php
Conclusion
Now that you’ve learned how to embed the Slimstat Analytics Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply