Simple Blog Stats Shortcodes

Below, you’ll find a detailed guide on how to add the Simple Blog Stats Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Simple Blog Stats Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Simple Blog Stats Plugin and the shortcodes it provides:

Plugin Icon
Simple Blog Stats

"Simple Blog Stats is a WordPress plugin designed to provide comprehensive and easy-to-understand statistics about your blog, helping you track its performance."

★★★★☆ (31) Active Installs: 5000+ Tested with: 6.3.2 PHP Version: 5.6.20
Included Shortcodes:
  • [sbs_posts]
  • [sbs_posts_alt]
  • [sbs_pages]
  • [sbs_drafts]
  • [sbs_comments]
  • [sbs_moderated]
  • [sbs_approved]
  • [sbs_users]
  • [sbs_roles]
  • [sbs_cats]
  • [sbs_tags]
  • [sbs_tax]
  • [sbs_tax_posts]
  • [sbs_updated]
  • [sbs_latest_posts]
  • [sbs_latest_comments]
  • []
  • [sbs_word_count]
  • [sbs_word_count_all]
  • [sbs_reading_time]
  • [sbs_cpt_count]
  • [sbs_cpts_count]
  • [sbs_media_count]
  • [sbs_blog_stats]
  • [sbs_logged_users]

Simple Blog Stats [sbs_posts] Shortcode

The Simple Blog Stats (SBS) shortcode is a versatile tool that retrieves and displays post statistics. This shortcode fetches post data based on specified criteria like type, status, category, and tag. It also allows for exclusions. The ‘sbs_posts’ shortcode can also limit the number of posts retrieved and format the count. If caching is enabled, the post count is stored temporarily to improve performance.

Shortcode: [sbs_posts]

Parameters

Here is a list of all possible sbs_posts shortcode parameters and attributes:

  • type – specifies the type of post to display
  • status – defines the status of the posts to show
  • cat – displays posts from a specific category
  • tag – shows posts associated with a specific tag
  • exclude – excludes certain posts from being displayed
  • exclude_cat – excludes posts from a specific category
  • number_format – formats the numbers displayed

Examples and Usage

Basic example – A simple usage of the ‘sbs_posts’ shortcode to display the number of published posts.

[sbs_posts]

Advanced examples

Displaying the number of published posts excluding certain post IDs. Here, the shortcode will exclude posts with IDs 5, 7, and 9.

[sbs_posts exclude="5,7,9"]

Displaying the number of published posts from a specific category. In this example, the shortcode will count posts from the category named ‘news’.

[sbs_posts cat="news"]

Displaying the number of published posts with a specific tag. Here, the shortcode will count posts with the tag ‘php’.

[sbs_posts tag="php"]

Combining different parameters. This example displays the number of published posts from the ‘news’ category, excluding posts with IDs 5, 7, and 9, and with the tag ‘php’.

[sbs_posts type="post" status="publish" cat="news" tag="php" exclude="5,7,9"]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_posts] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_posts', 'sbs_posts');

Shortcode PHP function:

function sbs_posts($attr, $content = null) {
	
	global $sbs_options;
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	extract(shortcode_atts(array(
		
		'type'          => 'post',
		'status'        => 'publish',
		'cat'           => null,
		'tag'           => null,
		'exclude'       => null,
		'exclude_cat'   => null,
		'number_format' => ',',
		
	), $attr));
	
	$limit = apply_filters('sbs_get_posts_limit', -1);
	
	$exclude     = $exclude     ? array_map('trim', explode(',', $exclude)) : null;

	$exclude_cat = $exclude_cat ? array_map('trim', explode(',', $exclude_cat)) : null;
	
	$args = array(
		'posts_per_page'   => $limit,
		'post_type'        => $type,
		'post_status'      => $status,
		'category_name'    => $cat,
		'tag'              => $tag,
		'fields'           => 'ids',
		'post__not_in'     => $exclude,
		'category__not_in' => $exclude_cat,
	);
	
	$args = apply_filters('sbs_posts_args', $args);
	
	$posts = get_posts($args);
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_post_count'))) {
			
			$count = count($posts);
			
			set_transient('sbs_post_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = count($posts);
		
	}
	
	return $sbs_options['count_posts_before'] . number_format($count, 0, '.', $number_format) . $sbs_options['count_posts_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_posts_alt] Shortcode

The Simple Blog Stats (SBS) shortcode ‘sbs_posts_alt’ displays the total number of published posts. The PHP function ‘sbs_posts_alt’ extracts the post type and status from the shortcode attributes. It then counts the total number of published posts and returns this total in a formatted manner.

Shortcode: [sbs_posts_alt]

Parameters

Here is a list of all possible sbs_posts_alt shortcode parameters and attributes:

  • type – Determines the type of content to count, default is ‘post’.
  • status – Specifies the status of posts to count, default is ‘publish’.

Examples and Usage

Basic example – Displaying the total number of published posts on your WordPress site.

[sbs_posts_alt]

Advanced examples

Displaying the total number of drafts on your WordPress site. In this example, we are changing the ‘status’ parameter to ‘draft’ to get the total number of draft posts.

[sbs_posts_alt status="draft"]

Displaying the total number of pages on your WordPress site. Here, we are changing the ‘type’ parameter to ‘page’ to get the total number of pages.

[sbs_posts_alt type="page"]

Displaying the total number of custom post types. In this example, we are changing the ‘type’ parameter to ‘your_custom_post_type’ to get the total number of your custom post types.

[sbs_posts_alt type="your_custom_post_type"]

Displaying the total number of private pages on your WordPress site. Here, we are changing both ‘type’ and ‘status’ parameters to ‘page’ and ‘private’ respectively to get the total number of private pages.

[sbs_posts_alt type="page" status="private"]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_posts_alt] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_posts_alt', 'sbs_posts_alt');

Shortcode PHP function:

function sbs_posts_alt($attr, $content = null) {
	
	extract(shortcode_atts(array(
		'type'   => 'post',
		'status' => 'publish',
	), $attr));
	
	$property = "$status";
	
	$total = wp_count_posts($type)->{$property};
	
	return number_format($total);
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_pages] Shortcode

The Simple Blog Stats (SBS) shortcode ‘sbs_pages’ is a tool that counts the number of published pages on your WordPress site. It checks if caching is enabled in the SBS settings. If caching is on, it retrieves the count from the cache, or updates it if the cache is outdated. If caching is off, it directly counts the number of published pages. The shortcode then returns this count, formatted according to the SBS settings.

Shortcode: [sbs_pages]

Examples and Usage

Basic example – An instance of the shortcode without any additional parameters. This will display the total number of published pages on your WordPress site.

[sbs_pages /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_pages] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_pages', 'sbs_pages');

Shortcode PHP function:

function sbs_pages() {
	
	global $sbs_options;
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	$count_pages = wp_count_posts('page');
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_page_count'))) {
			
			$count = intval($count_pages->publish);
			
			set_transient('sbs_page_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = intval($count_pages->publish);
		
	}
	
	return $sbs_options['count_pages_before'] . number_format($count) . $sbs_options['count_pages_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_drafts] Shortcode

The Simple Blog Stats (SBS) shortcode is a useful tool for displaying the number of draft posts on your WordPress site. This shortcode uses the function ‘sbs_drafts’ to access global options and count the draft posts. It checks if cache is enabled, and if so, it retrieves the count from cache. If not, it directly counts the drafts. The count is then formatted and returned, allowing you to easily display draft post stats on your site.

Shortcode: [sbs_drafts]

Examples and Usage

Basic example – A simple usage of the shortcode to display the number of drafts in your blog.

[sbs_drafts /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_drafts] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_drafts', 'sbs_drafts');

Shortcode PHP function:

function sbs_drafts() {
	
	global $sbs_options;
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	$count_drafts = wp_count_posts();
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_draft_count'))) {
			
			$count = intval($count_drafts->draft);
			
			set_transient('sbs_draft_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = intval($count_drafts->draft);
		
	}
	
	return $sbs_options['count_drafts_before'] . number_format($count) . $sbs_options['count_drafts_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_comments] Shortcode

The Simple Blog Stats shortcode, ‘sbs_comments’, is used to display the total number of comments on a WordPress website. It can also show the comment count for a specific category if a ‘cat’ attribute is provided. The shortcode checks if caching is enabled. If so, it retrieves the comment count from the cache. If not, it directly calculates the count. It then returns the count, formatted according to the options set in the plugin’s settings.

Shortcode: [sbs_comments]

Parameters

Here is a list of all possible sbs_comments shortcode parameters and attributes:

  • cat – specifies the category for counting comments

Examples and Usage

Basic example – A straightforward usage of the shortcode to display the total number of comments on your blog.

[sbs_comments /]

Advanced examples

Using the shortcode to display the total number of comments for a specific category by providing the category id. This can be useful if you want to showcase the engagement for a particular category on your blog.

[sbs_comments cat=3 /]

Using the shortcode to display the total number of comments and format the output with custom text before and after the count. This can be done by setting the ‘count_comments_before’ and ‘count_comments_after’ options in the plugin settings. This is useful for adding context or extra information to the count.

[sbs_comments count_comments_before="Total Comments: " count_comments_after=" Keep them coming!" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_comments] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_comments', 'sbs_comments');

Shortcode PHP function:

function sbs_comments($attr, $content = null) {
	
	global $sbs_options;
	
	extract(shortcode_atts(array('cat' => null), $attr));
	
	if ($cat) {
		
		$count_comments = sbs_category_comments($cat);
		
	} else {
		
		$count_comments = wp_count_comments();
		
		$count_comments = $count_comments->total_comments;
		
	}
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_comments_total_count'))) {
			
			$count = intval($count_comments);
			
			set_transient('sbs_comments_total_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = intval($count_comments);
		
	}
	
	return $sbs_options['count_comments_before'] . number_format($count) . $sbs_options['count_comments_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_moderated] Shortcode

The Simple Blog Stats (SBS) shortcode is used to display the number of moderated comments on a website. It first checks if caching is enabled. If it is, it retrieves the comment count from the cache. If the count is not in the cache or caching is off, it calculates the count directly. The count is then formatted and returned with optional text before and after.

Shortcode: [sbs_moderated]

Examples and Usage

Basic example – The shortcode displays the number of moderated comments on your WordPress site.

[sbs_moderated /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_moderated] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_moderated', 'sbs_moderated');

Shortcode PHP function:

function sbs_moderated() {
	
	global $sbs_options;
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	$count_moderated = wp_count_comments();
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_comments_moderated_count'))) {
			
			$count = intval($count_moderated->moderated);
			
			set_transient('sbs_comments_moderated_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = intval($count_moderated->moderated);
		
	}
	
	return $sbs_options['count_moderated_before'] . number_format($count) . $sbs_options['count_moderated_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_approved] Shortcode

The ‘sbs_approved’ shortcode from the Simple Blog Stats plugin is designed to display the count of approved comments on your WordPress site. This shortcode fetches the total number of approved comments, optionally applies number formatting, and displays the count. If caching is enabled, it stores the count in a transient to improve performance.

Shortcode: [sbs_approved]

Parameters

Here is a list of all possible sbs_approved shortcode parameters and attributes:

  • number_format – It defines the separator for the thousands place in the count.

Examples and Usage

Basic example – A simple usage of the shortcode to display the number of approved comments on your blog.

[sbs_approved /]

Advanced examples

Displaying the number of approved comments with a specific number format. In this case, we use the ‘.’ as the number format which will separate the digits of the count with a dot.

[sbs_approved number_format='.' /]

Another advanced usage of the shortcode is to display the number of approved comments with a specific number format and cache enabled. The cache will store the count for 12 hours to improve performance. The number format is set to ‘,’.

[sbs_approved number_format=',' sbs_enable_cache='true' /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_approved] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_approved', 'sbs_approved');

Shortcode PHP function:

function sbs_approved($attr, $content = null) {
	
	global $sbs_options;
	
	extract(shortcode_atts(array(
		
		'number_format' => ',',
		
	), $attr));
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	$count_approved = wp_count_comments();
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_comments_approved_count'))) {
			
			$count = intval($count_approved->approved);
			
			set_transient('sbs_comments_approved_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = intval($count_approved->approved);
		
	}
	
	return $sbs_options['count_approved_before'] . number_format($count, 0, '.', $number_format) . $sbs_options['count_approved_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_users] Shortcode

The Simple Blog Stats shortcode ‘sbs_users’ is designed to fetch and display the total number of registered users on your WordPress site. This shortcode works by calling the ‘sbs_users’ function which checks if caching is enabled. If so, it retrieves the user count from cache. If not, or if the cache is outdated, it counts the users directly. The total user count is then formatted and returned.

Shortcode: [sbs_users]

Examples and Usage

Basic example – The shortcode displays the total number of users on your WordPress site.

[sbs_users /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_users] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_users', 'sbs_users');

Shortcode PHP function:

function sbs_users() {
	
	global $sbs_options;
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	$count_users = count_users();
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_user_count'))) {
			
			$count = intval($count_users['total_users']);
			
			set_transient('sbs_user_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = intval($count_users['total_users']);
		
	}
	
	return $sbs_options['count_users_before'] . number_format($count) . $sbs_options['count_users_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_roles] Shortcode

The Simple Blog Stats shortcode ‘sbs_roles’ displays the count of users for each role on your WordPress site. It creates a list of all user roles and their corresponding counts. If a specific ‘role’ is provided, it shows only the count for that role. The ‘txt’ attribute allows for custom labels.

Shortcode: [sbs_roles]

Parameters

Here is a list of all possible sbs_roles shortcode parameters and attributes:

  • role – Defines the user role to display statistics for
  • txt – Custom text to replace the default user role label

Examples and Usage

Basic example – Displaying all available roles and their count on your WordPress site.

[sbs_roles]

Advanced examples

Displaying the count of a specific role. For instance, showing the number of ‘authors’ on your site.

[sbs_roles role="author"]

Adding a custom label to the count. For instance, showing the number of ‘authors’ on your site with a label ‘Content Creators’.

[sbs_roles role="author" txt="Content Creators"]

Displaying the count of a specific role without any label. This can be achieved by setting the ‘txt’ attribute to ‘null’.

[sbs_roles role="author" txt="null"]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_roles] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_roles', 'sbs_roles');

Shortcode PHP function:

function sbs_roles($attr, $content = null) {
	
	global $sbs_options;
	
	extract(shortcode_atts(array(
		'role' => 'all',
		'txt'  => '',
	), $attr));
	
	$count_users = count_users();
	
	$roles = isset($count_users['avail_roles']) ? $count_users['avail_roles'] : false;
	
	unset($roles['none']);
	
	if (!isset($role) || empty($role) || $role === 'all') {
		
		$return = '<ul>';
		
		foreach ($roles as $key => $value) {
			
			$label = function_exists('ngettext') ? ngettext(ucfirst($key), ucfirst($key) . esc_html__('s', 'simple-blog-stats'), intval($value)) : ucfirst($key) .'s';
			
			if ($txt) $label = $txt;
			
			if ($txt === 'null') $label = '';
			
    		$return .= '<li>'. number_format($value) .' '. $label .'</li>';
    		
		}
		
		$return .= '</ul>';
		
	} else {
		
		$return = '';
		
		foreach ($roles as $key => $value) {
			
			if (strtolower($key) === strtolower($role)) {
				
				$label = function_exists('ngettext') ? ngettext(ucfirst($role), ucfirst($role) . esc_html__('s', 'simple-blog-stats'), intval($value)) : ucfirst($role) .'s';
				
				if ($txt) $label = $txt;
				
				if ($txt === 'null') $label = '';
				
				$return .= number_format($value) .' '. $label;
				
    		}
    		
		}
		
	}
	
	return $sbs_options['count_roles_before'] . $return . $sbs_options['count_roles_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_cats] Shortcode

The Simple Blog Stats (SBS) plugin shortcode ‘sbs_cats’ displays the total number of categories on your WordPress site. It uses the ‘wp_list_categories’ function to fetch all categories and then counts them. The result is then formatted and returned for display.

Shortcode: [sbs_cats]

Examples and Usage

Basic example – Display the number of categories in your blog.

[sbs_cats /]

With this shortcode, you can easily display the total number of categories in your blog. It uses the ‘wp_list_categories’ function to retrieve all the categories and then counts them. The result is then formatted with ‘number_format’ function.

Advanced examples

Display the number of categories with custom text before and after the count.

[sbs_cats count_cats_before="We have " count_cats_after=" categories in our blog." /]

Here, the shortcode is extended with two parameters: ‘count_cats_before’ and ‘count_cats_after’. These parameters allow you to add custom text before and after the category count. In this example, the output might look something like “We have 10 categories in our blog.”

Remember, the ‘sbs_cats’ shortcode does not accept any parameters directly. The parameters ‘count_cats_before’ and ‘count_cats_after’ are actually options that need to be set in the plugin’s settings page. Once they are set, they can be used in the shortcode as shown in the advanced example above.

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_cats] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_cats', 'sbs_cats');

Shortcode PHP function:

function sbs_cats() {
	global $sbs_options;
	$cats = wp_list_categories('title_li=&style=none&echo=0');
	$cats_parts = explode('<br />', $cats);
	$cats_count = count($cats_parts) - 1;
	return $sbs_options['count_cats_before'] . number_format($cats_count) . $sbs_options['count_cats_after'];
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_tags] Shortcode

The Simple Blog Stats (SBS) shortcode ‘sbs_tags’ is designed to display the total number of tags on your WordPress site. This shortcode fetches the count of ‘post_tag’, which represents all the tags. It uses the ‘wp_count_terms’ function to retrieve this data. The result is then formatted and returned, sandwiched between ‘count_tags_before’ and ‘count_tags_after’ options.

Shortcode: [sbs_tags]

Examples and Usage

Basic example – A simple usage of the ‘sbs_tags’ shortcode to display the total number of tags on your WordPress site.

[sbs_tags /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_tags] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_tags', 'sbs_tags');

Shortcode PHP function:

function sbs_tags() {
	global $sbs_options;
	return $sbs_options['count_tags_before'] . number_format(wp_count_terms('post_tag')) . $sbs_options['count_tags_after'];
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_tax] Shortcode

The Simple Blog Stats (SBS) shortcode ‘sbs_tax’ is used to display the total count of taxonomy terms. It fetches the count before and after the term, based on the SBS options set.

Shortcode: [sbs_tax]

Parameters

Here is a list of all possible sbs_tax shortcode parameters and attributes:

  • tax – specifies the taxonomy for which terms are counted
  • count_tax_before – text or HTML to display before the count
  • count_tax_after – text or HTML to display after the count

Examples and Usage

Basic example – A shortcode that counts and displays the number of terms for a specified taxonomy.

[sbs_tax tax="category"]

Advanced examples

Adding text before and after the count using the ‘count_tax_before’ and ‘count_tax_after’ options in the sbs_options array. This example assumes that you have added ‘Posts in ‘ as the ‘count_tax_before’ option and ‘ categories’ as the ‘count_tax_after’ option. The shortcode will display the text ‘Posts in X categories’ where X is the count of terms in the specified taxonomy.

[sbs_tax tax="category"]

Using the shortcode to count the number of terms in a custom taxonomy. Replace ‘your_taxonomy’ with the name of your custom taxonomy.

[sbs_tax tax="your_taxonomy"]

Using the shortcode to count the number of terms in multiple taxonomies. Just separate the taxonomy names with a comma. This example counts the number of terms in the ‘category’ and ‘post_tag’ taxonomies.

[sbs_tax tax="category,post_tag"]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_tax] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_tax', 'sbs_tax');

Shortcode PHP function:

function sbs_tax($attr, $content = null) {
	
	global $sbs_options;
	
	$before = isset($sbs_options['count_tax_before']) ? $sbs_options['count_tax_before'] : '';
	$after  = isset($sbs_options['count_tax_after'])  ? $sbs_options['count_tax_after']  : '';
	
	extract(shortcode_atts(array('tax' => null), $attr));
	
	$tax = intval(wp_count_terms($tax));
	
	return $before . number_format($tax) . $after;
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_tax_posts] Shortcode

The Simple Blog Stats shortcode ‘sbs_tax_posts’ is used to display the number of posts related to a specific taxonomy term. This shortcode fetches posts based on the parameters set like post type, status, taxonomy, and term. It can also limit the number of posts retrieved. If caching is enabled, the shortcode stores the post count in a transient to improve performance.

Shortcode: [sbs_tax_posts]

Parameters

Here is a list of all possible sbs_tax_posts shortcode parameters and attributes:

  • type – defines the type of content, default is ‘post’
  • status – sets the post status, default is ‘publish’
  • tax – sets the taxonomy for the query
  • field – specifies the term field to be used, default is ‘slug’
  • terms – defines the terms to be included in the query

Examples and Usage

Basic example – Displays the number of posts published in the ‘post’ type.

[sbs_tax_posts]

Advanced examples

Displays the number of published posts in the ‘product’ type.

[sbs_tax_posts type='product']

Displays the number of ‘draft’ posts in the ‘post’ type.

[sbs_tax_posts status='draft']

Displays the number of posts in the ‘post’ type with the ‘category’ taxonomy, using the slug field, for the terms ‘news’ and ‘updates’.

[sbs_tax_posts tax='category' field='slug' terms='news,updates']

Displays the number of posts in the ‘post’ type with the ‘tag’ taxonomy, using the id field, for the terms with ids ‘3’ and ‘5’.

[sbs_tax_posts tax='tag' field='id' terms='3,5']

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_tax_posts] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_tax_posts', 'sbs_tax_posts');

Shortcode PHP function:

function sbs_tax_posts($attr, $content = null) {
	
	global $sbs_options;
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	extract(shortcode_atts(array(
		'type'   => 'post',
		'status' => 'publish',
		'tax'    => null,
		'field'  => 'slug',
		'terms'  => null,
	), $attr));
	
	$explode = explode(',', $terms);
	
	$terms = array();
	
	foreach ($explode as $exp) $terms[] = trim($exp, ', ');
	
	$limit = apply_filters('sbs_get_posts_limit', -1);
	
	$args = array(
		'posts_per_page' => $limit,
		'post_type'      => $type,
		'post_status'    => $status,
		'fields'         => 'ids',
		'tax_query'      => array(array('taxonomy' => $tax, 'field' => $field, 'terms' => $terms)),
	);
	
	$posts = get_posts($args);
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_tax_posts_count'))) {
			
			$count = count($posts);
			
			set_transient('sbs_tax_posts_count', $count, 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = count($posts);
		
	}
	
	return number_format($count);
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_updated] Shortcode

The Simple Blog Stats shortcode ‘sbs_updated’ displays the last updated post’s date and time. It filters post types and statuses, and can format the output. The ‘sbs_updated’ shortcode queries the most recent post, retrieves the date and time it was last modified, and returns this data. If no posts are found, it defaults to ‘awhile ago’. Shortcode: [sbs_updated]

Shortcode: [sbs_updated]

Parameters

Here is a list of all possible sbs_updated shortcode parameters and attributes:

  • format_date – Determines the format of the displayed date
  • format_time – Sets the format of the time displayed

Examples and Usage

Basic example – Here’s how to display the last updated date of a post using the shortcode without any parameters.

[sbs_updated /]

Advanced examples

Display the last updated date of a post in a specific format. In this example, we are using ‘F j, Y’ as the date format, which will display the date in a format like ‘June 4, 2021’.

[sbs_updated format_date='F j, Y' /]

Display the last updated date and time of a post in specific formats. In this example, we are using ‘F j, Y’ as the date format and ‘g:i a’ as the time format, which will display the date and time in a format like ‘June 4, 2021 @ 2:30 pm’.

[sbs_updated format_date='F j, Y' format_time='g:i a' /]

If you want to disable the time, you can set the ‘format_time’ parameter to ‘disable’. This will only display the date.

[sbs_updated format_date='F j, Y' format_time='disable' /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_updated] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_updated', 'sbs_updated');

Shortcode PHP function:

function sbs_updated($attr) {
	
	global $sbs_options;
	
	extract(shortcode_atts(array(
		'format_date'  => '',
		'format_time'  => '',
	), $attr));
	
	$post_type = apply_filters('sbs_updated_post_type', array('post'));
	
	$post_status = apply_filters('sbs_updated_post_status', array('publish'));
	
	$query = array(
		'post_type'      => $post_type,
		'post_status'    => $post_status,
		'posts_per_page' => 1,
		'orderby'        => 'date',
	);
	
	$recent = new WP_Query($query);
	
	if ($recent->have_posts()) {
		
		while ($recent->have_posts()) {
			
			$recent->the_post();
			
			$post_date = get_the_modified_date($format_date);
			
			$post_time = ($format_time === 'disable') ? '' : ' <span class="sbs-site-updated-time">@ '. get_the_time($format_time) .'</span>';
			
			$last_update = $post_date . $post_time;
			
			$last_update = apply_filters('sbs_last_update', $last_update, $post_date, $post_time);
			
		}
		
		return $sbs_options['site_updated_before'] . sanitize_text_field($last_update) . $sbs_options['site_updated_after'];
		
	} else {
		
		return $sbs_options['site_updated_before'] . sanitize_text_field('awhile ago') . $sbs_options['site_updated_after'];
		
	}
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_latest_posts] Shortcode

The Simple Blog Stats shortcode, ‘sbs_latest_posts’, displays a list of the most recent posts on your blog. It fetches a specified number of latest posts, trims their content to a set length, and presents them in an unordered list. The list includes the post title, a short excerpt, and a ‘read more’ link. If no new posts are found, it displays ‘nothing new’.

Shortcode: [sbs_latest_posts]

Examples and Usage

Basic Example – Displaying the latest posts using the ‘sbs_latest_posts’ shortcode. This shortcode will display the most recent posts based on the settings defined in the $sbs_options array.

[sbs_latest_posts]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_latest_posts] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_latest_posts', 'sbs_latest_posts');

Shortcode PHP function:

function sbs_latest_posts($d = '') {
	global $sbs_options;
	$posts_number = $sbs_options['number_of_posts'];
	$post_length  = $sbs_options['post_length'];
	$latest = new WP_Query("showposts=$posts_number&orderby=date&post_status=publish");
	if ($latest->have_posts()) {
		$latest_posts = '<ul id="sbs-posts">';
		while ($latest->have_posts()) {
			$latest->the_post();
			$post_content = get_the_content();
			$post_excerpt = preg_replace('/\s+?(\S+)?$/', '', substr($post_content, 0, $post_length));
			$post_display = strip_tags($post_excerpt, '<p>');
			$latest_posts .= '<li class="sbs-post"><a href="' . get_permalink() . '">' . the_title_attribute(array('echo'=>0)) . '</a> ';
			$latest_posts .= '<span>' . $post_display . ' <small>[...]</small></span></li>';
		}
		$latest_posts .= '</ul>';
		return $sbs_options['latest_posts_before'] . $latest_posts . $sbs_options['latest_posts_after'];
	} else {
		return $sbs_options['latest_posts_before'] . 'nothing new' . $sbs_options['latest_posts_after'];
	}
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_latest_comments] Shortcode

The Simple Blog Stats shortcode ‘sbs_latest_comments’ displays the latest comments on your blog. It retrieves a specified number of recent comments, truncates them to a set length, and lists them with the author’s name, comment date, and a link to the full comment.

Shortcode: [sbs_latest_comments]

Examples and Usage

Basic example – Show the latest comments on your blog posts

[sbs_latest_comments number_of_comments=5 comment_length=100]

With this shortcode, you can display the 5 most recent comments on your blog posts. Each comment will be truncated to 100 characters.

Advanced examples

Display the latest comments, but increase the number of comments shown and the length of each comment.

[sbs_latest_comments number_of_comments=10 comment_length=200]

With this shortcode, you can display the 10 most recent comments on your blog posts. Each comment will be truncated to 200 characters.

Display the latest comments with a custom before and after text.

[sbs_latest_comments latest_comments_before="Here are the latest comments:" latest_comments_after="End of comments."]

With this shortcode, you can display the latest comments with a custom text before and after the comments list. The before text says “Here are the latest comments:” and the after text says “End of comments.”

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_latest_comments] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_latest_comments', 'sbs_latest_comments');

Shortcode PHP function:

function sbs_latest_comments() {
	global $sbs_options;
	$comments_number = $sbs_options['number_of_comments'];
	$comment_length  = $sbs_options['comment_length'];

	$recent_comments = get_comments(array('number'=>$comments_number, 'status'=>'approve'));
	$comments = '<ul id="sbs-comments">';
	foreach ($recent_comments as $recent_comment) {
		$comment_id        = $recent_comment->comment_ID;
		$comment_date      = $recent_comment->comment_date;
		$comment_author    = $recent_comment->comment_author;

		$comment_content   = $recent_comment->comment_content;
		$comment_excerpt   = preg_replace('/\s+?(\S+)?$/', '', substr($comment_content, 0, $comment_length));

		$line_breaks       = array("\r\n", "\n", "\r");
		$comment_display   = str_replace($line_breaks, " ", $comment_excerpt);
		$comment_display   = esc_attr($comment_display);

		$comment_post_id   = $recent_comment->comment_post_ID;
		$comment_permalink = get_permalink($comment_post_id);

		$comments .= '<li class="sbs-comment">';
		$comments .= '<a href="' . $comment_permalink . '#comment-' . $comment_id . '" title="Posted: ' . $comment_date . '">' . $comment_author . '</a>: ';
		$comments .= '<span>' . $comment_display . ' <small>[...]</small></span></li>';
	}
	$comments .= '</ul>';
	return $sbs_options['latest_comments_before'] . $comments . $sbs_options['latest_comments_after'];	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [null] Shortcode

The Simple Blog Stats shortcode, [sbs_word_count_custom], is designed to fetch and count words in a custom field of a specified post. It extracts ‘post_id’, ‘key’, and ‘single’ attributes from the shortcode. If ‘post_id’ or ‘key’ is empty, it returns nothing. It retrieves the custom field value using ‘get_post_meta’ function and strips any HTML tags. Finally, it counts the words and returns the count in a formatted manner.

Shortcode: [null]

Parameters

Here is a list of all possible null shortcode parameters and attributes:

  • post_id – The unique identifier of the post.
  • key – The specific custom field name to retrieve.
  • single – If set to true, returns a single value, else an array.

Examples and Usage

Basic example – Display the word count of a custom field in a specific post using its post ID and custom field key.

[sbs_word_count_custom post_id="123" key="my_custom_field"]

Advanced examples

Display the word count of a custom field in a specific post, but this time, we’re setting the ‘single’ parameter to false. This means the shortcode will return an array of values if the custom field has multiple values.

[sbs_word_count_custom post_id="123" key="my_custom_field" single="false"]

Display the word count of a custom field without specifying a post ID. This will use the global $post object, which means it will display the word count for the custom field of the current post being viewed.

[sbs_word_count_custom key="my_custom_field"]

PHP Function Code

In case you have difficulties debugging what causing issues with [null] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_word_count_custom', 'sbs_word_count_custom');

Shortcode PHP function:

function sbs_word_count_custom($atts) {
	
	extract(shortcode_atts(array(
		'post_id' => null,
		'key'     => '',
		'single'  => true
	), $atts));
	
	if (empty($post_id) || empty($key)) return;
	
	$custom_field = get_post_meta($post_id, $key, $single);
	
	if (empty($custom_field)) return;
	
	$custom_field = strip_tags($custom_field);
	
	$count = str_word_count($custom_field);
	
	return number_format($count);
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_word_count] Shortcode

The Simple Blog Stats shortcode, ‘sbs_word_count’, calculates the word count of a post. It first checks if a specific ID is given, if not, it uses the current post’s ID. The shortcode then strips all HTML tags from the post content, and calculates the word count. It also removes any instances of itself to avoid recursion. Finally, it returns the word count, formatted according to the plugin’s settings.

Shortcode: [sbs_word_count]

Parameters

Here is a list of all possible sbs_word_count shortcode parameters and attributes:

  • id – numerical value representing the specific post

Examples and Usage

Basic example – Displays the word count of the current post or page.

[sbs_word_count]

Advanced examples

Display the word count of a specific post or page by specifying the ID attribute. For instance, to show the word count of the post with ID 42:

[sbs_word_count id=42]

Include the shortcode within the content of another post or page. This will exclude the word count of the shortcode itself. For example:

[sbs_word_count id=99]

Note: In the above example, the shortcode [sbs_word_count id=99] will not be counted as part of the word count of the post or page in which it’s included.

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_word_count] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_word_count', 'sbs_word_count');

Shortcode PHP function:

function sbs_word_count($atts) {
	
	global $sbs_options;
	
	$args = shortcode_atts(array('id' => false), $atts);
    
	$id = (isset($args['id']) && !empty($args['id']) && is_numeric($args['id'])) ? $args['id'] : get_the_ID();
	
	$post = get_post($id); 
	
	$content = isset($post->post_content) ? strip_tags($post->post_content) : null;
	
	$count = 0;
	
	if ($content) {
		
		$content = preg_replace('/(\[sbs_word_count(.*)\])/', '', $content);
		
		$count = str_word_count($content);
		
	}
	
	$count = is_int($count) ? $count : 0;
	
	return $sbs_options['count_words_before'] . number_format($count) .  $sbs_options['count_words_after'];
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_word_count_all] Shortcode

Simple Blog Stats shortcode is a powerful tool that counts the total words across all your published content. The shortcode fetches all posts, removes its own shortcode instances, and sums up the word count. It also supports caching, which can significantly improve performance by storing the word count for 12 hours. This count is then formatted and returned, either wrapped in custom text or as a standalone number.

Shortcode: [sbs_word_count_all]

Examples and Usage

Basic example – Displaying the total word count across all posts on your blog.

[sbs_word_count_all]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_word_count_all] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_word_count_all', 'sbs_word_count_all');

Shortcode PHP function:

function sbs_word_count_all($wrap) {
	
	$disable = apply_filters('sbs_word_count_all_disable', false);
	
	if ($disable) return;
	
	//
	
	global $sbs_options;
	
	$cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false;
	
	$limit = apply_filters('sbs_get_posts_limit', -1);
	
	$post_type = apply_filters('sbs_word_count_all_post_type', 'any');
	
	$args = array(
		'post_type'      => $post_type, 
		'post_status'    => 'publish', 
		'posts_per_page' => $limit,
		'fields'         => 'ids',
	);
	
	$posts = get_posts($args);
	
	if (!$posts) return;
	
	if ($cache) {
		
		if (false === ($count = get_transient('sbs_word_count'))) {
			
			$count = 0;
			
			foreach ($posts as $id) {
				
				$post = get_post($id);
				
				$content = isset($post->post_content) ? $post->post_content : null;
				
				$content = preg_replace('/(\[sbs_word_count(.*)\])/', '', $content);
				
				$count += str_word_count($content, 0);
				
			}
			
			set_transient('sbs_word_count', number_format(floatval($count)), 12 * HOUR_IN_SECONDS);
			
		}
		
	} else {
		
		$count = 0;
		
		foreach ($posts as $id) {
			
			$post = get_post($id);
			
			$content = isset($post->post_content) ? $post->post_content : null;
			
			$content = preg_replace('/(\[sbs_word_count(.*)\])/', '', $content);
			
			$count += str_word_count($content, 0);
			
		}
		
	}
	
	wp_reset_postdata();
	
	$count = is_int($count) ? $count : 0;
	
	$output = number_format($count);
	
	$output = ($wrap) ? $sbs_options['count_words_all_before'] . $output . $sbs_options['count_words_all_after'] : $output;
	
	return $output;
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_reading_time] Shortcode

The Simple Blog Stats shortcode is a useful tool for calculating reading time for blog posts. It strips out HTML tags, counts words, and estimates reading time based on a 200 words per minute reading speed.

Shortcode: [sbs_reading_time]

Parameters

Here is a list of all possible sbs_reading_time shortcode parameters and attributes:

  • id – The specific number that identifies the post

Examples and Usage

Basic example – Display the reading time of the current post.

[sbs_reading_time /]

Advanced examples

Display the reading time of a specific post by referencing the post’s ID. This can be useful when you want to display the reading time of a particular post on a different page or post.

[sbs_reading_time id=5 /]

Display the reading time of multiple posts by referencing their IDs. Just make sure to separate each ID with a comma. This can be useful when you want to display the reading times of several related posts on a single page or post.

[sbs_reading_time id="5,6,7" /]

Please note that the ‘sbs_reading_time’ shortcode will only work if the ‘Simple Blog Stats’ plugin is installed and activated on your WordPress site.

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_reading_time] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_reading_time', 'sbs_reading_time');

Shortcode PHP function:

function sbs_reading_time($atts) {
	
	global $sbs_options;
	
	$args = shortcode_atts(array('id' => false), $atts);
    
	$id = (isset($args['id']) && !empty($args['id']) && is_numeric($args['id'])) ? $args['id'] : get_the_ID();
	
	$post = get_post($id); 
	
	$content = isset($post->post_content) ? strip_tags($post->post_content) : null;
	
	$output = '';
	
	if ($content) {
		
		$content = preg_replace('/(\[sbs_reading_time(.*)\])/', '', $content);
		
		$count = str_word_count($content);
		
		$read_time = ceil($count / 200);
		
		$read_time = number_format($read_time);
		
		$units = ($read_time == 1) ? esc_html__(' minute', 'simple-blog-stats') : esc_html__(' minutes', 'simple-blog-stats');
		
		$output = $read_time . $units;
		
	}
	
	return $output;
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_cpt_count] Shortcode

The Simple Blog Stats (SBS) shortcode, ‘sbs_cpt_count’, is used to display the count of custom post types on your site. It extracts the attributes ‘cpt’ and ‘txt’ from the shortcode, checks the post type, and counts the number of published posts.

Shortcode: [sbs_cpt_count]

Parameters

Here is a list of all possible sbs_cpt_count shortcode parameters and attributes:

  • cpt – Specifies the type of content to count
  • txt – Replaces the name of the content type with a custom text

Examples and Usage

Basic example – The shortcode displays the count of a specific custom post type. It defaults to ‘post’ if no custom post type is specified.

[sbs_cpt_count cpt='post']

Advanced examples

Displaying the count of a custom post type, ‘products’, with a custom text ‘Items’.

[sbs_cpt_count cpt='products' txt='Items']

Using the shortcode to display the count of a custom post type, ‘events’, without any text.

[sbs_cpt_count cpt='events' txt='null']

Please note, the ‘txt’ parameter overrides the name of the post type with custom text. If ‘txt’ is set to ‘null’, no text will be displayed.

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_cpt_count] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_cpt_count', 'sbs_cpt_count');

Shortcode PHP function:

function sbs_cpt_count($atts) {
	
	global $sbs_options;
	
	$before = isset($sbs_options['sbs_cpt_before']) ? $sbs_options['sbs_cpt_before'] : '';
	$after  = isset($sbs_options['sbs_cpt_after'])  ? $sbs_options['sbs_cpt_after']  : '';
	
	extract(shortcode_atts(array(
		'cpt' => 'post',
		'txt' => '',
	), $atts));
	
	$post = get_post_type_object($cpt);
	
	$name = isset($post->labels->name) ? $post->labels->name : null;
	
	if ($txt) $name = $txt;
	
	if ($txt === 'null') $name = '';
	
	$count = wp_count_posts($cpt);
	
	$publish = ($count && property_exists($count, 'publish')) ? number_format($count->publish) .' '. $name : 0 .' '. $name;
	
	return $before . $publish . $after;
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_cpts_count] Shortcode

The Simple Blog Stats shortcode ‘sbs_cpts_count’ displays a list of custom post types and their respective counts. It retrieves all the public and non-built-in post types, counts the published posts for each type, and formats the output in a list.

Shortcode: [sbs_cpts_count]

Examples and Usage

Basic example – Display a list of custom post types and their count on your site.

[sbs_cpts_count /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_cpts_count] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_cpts_count', 'sbs_cpts_count');

Shortcode PHP function:

function sbs_cpts_count($atts) {
	
	global $sbs_options;
	
	$before = isset($sbs_options['sbs_cpts_before']) ? $sbs_options['sbs_cpts_before'] : '';
	$after  = isset($sbs_options['sbs_cpts_after'])  ? $sbs_options['sbs_cpts_after']  : '';
	
	$post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects', 'and');
	
	$html = '<ul>';
	
	foreach ($post_types as $post_type) {
		
		$num_posts = wp_count_posts($post_type->name);
		
		$num = number_format_i18n($num_posts->publish);
		
		$text = _n($post_type->labels->singular_name, $post_type->labels->name, intval($num_posts->publish));
		
		$html .= '<li>'. number_format(floatval($num)) .' '. esc_html($text) .'</li>';
		
	}
	
	$html .= '</ul>';
	
	return $before . $html . $after;
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_media_count] Shortcode

The Simple Blog Stats (SBS) shortcode ‘sbs_media_count’ provides a count of media files. It categorizes them into image, video, or other types based on file extensions. It applies filters to adjust the count and formatting. The shortcode can be customized with ‘before’ and ‘after’ text, allowing for personalized display.

Shortcode: [sbs_media_count]

Parameters

Here is a list of all possible sbs_media_count shortcode parameters and attributes:

  • type – Defines the type of media to be counted.
  • txt – Optional text to display after the count.

Examples and Usage

Basic example – Display the number of all media files in your site.

[sbs_media_count type="all" /]

Advanced examples – Below are a few more examples showcasing the versatility of the sbs_media_count shortcode.

Display the number of image files on your site. This includes jpg, gif, png, bmp, tif, and ico file types.

[sbs_media_count type="image" /]

Display the number of video files on your site. This includes asf, wmv, wmx, avi, divx, flv, mov, mpeg, mp4, ogv, webm, and mkv file types.

[sbs_media_count type="video" /]

Define specific file types to count. In this example, we count the number of jpeg and png files.

[sbs_media_count type="jpeg,png" /]

Add a custom text after the media count. In this example, we add the text “files” after the count.

[sbs_media_count type="all" txt="files" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_media_count] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_media_count', 'sbs_media_count');

Shortcode PHP function:

function sbs_media_count($atts) {
	
	global $sbs_options;
	
	$before = isset($sbs_options['sbs_media_before']) ? $sbs_options['sbs_media_before'] : '';
	$after  = isset($sbs_options['sbs_media_after'])  ? $sbs_options['sbs_media_after']  : '';
	
	extract(shortcode_atts(array(
		'type' => 'image',
		'txt' => '',
	), $atts));
	
	$name = $txt ? ' '. $txt : '';
	
	//
	
	$mime = array();
	
	$mimes = get_allowed_mime_types();
	
	if ($type === 'all') {
		
		$mime = $mimes;
		
	} elseif ($type === 'image') {
		
		$mime = array(
			'jpg|jpeg|jpe' => 'image/jpeg',
			'gif'          => 'image/gif',
			'png'          => 'image/png',
			'bmp'          => 'image/bmp',
			'tif|tiff'     => 'image/tiff',
			'ico'          => 'image/x-icon',
		);
		
	} elseif ($type === 'video') {
		
		$mime = array(
			'asf|asx'      => 'video/x-ms-asf',
			'wmv'          => 'video/x-ms-wmv',
			'wmx'          => 'video/x-ms-wmx',
			'wm'           => 'video/x-ms-wm',
			'avi'          => 'video/avi',
			'divx'         => 'video/divx',
			'flv'          => 'video/x-flv',
			'mov|qt'       => 'video/quicktime',
			'mpeg|mpg|mpe' => 'video/mpeg',
			'mp4|m4v'      => 'video/mp4',
			'ogv'          => 'video/ogg',
			'webm'         => 'video/webm',
			'mkv'          => 'video/x-matroska',
		);
		
	} else {
		
		$types = array_map('trim', explode(',', $type));
		
		foreach ($types as $type) {
			
			foreach ($mimes as $key => $value) {
				
				$exts = array_map('trim', explode('|', $key));
				
				foreach ($exts as $ext) {
					
					if ($ext === $type) {
						
						$mime[$key] = $mimes[$key];
						
					}
					
				}
				
			}
			
		}
		
	}
	
	$mime = apply_filters('sbs_media_count_mime', $mime, $type);
	
	$args = array(
		
		'post_type'      => 'attachment',
		'post_mime_type' => $mime,
		'post_status'    => 'inherit',
		'posts_per_page' => -1,
		'fields'         => 'ids',
		
	);
	
	$query = new WP_Query($args);
	
	$count = isset($query->post_count) ? $query->post_count : 0;
	
	$commas = apply_filters('sbs_include_commas', true);
	
	$count = $commas ? number_format($count) : $count;
	
	return $before . $count . $name . $after;
	
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_blog_stats] Shortcode

The Simple Blog Stats shortcode provides a comprehensive overview of your blog’s statistics. It counts and displays the number of posts, pages, drafts, words, total comments, comments in queue, approved comments, registered users, categories, and tags on your site.

Shortcode: [sbs_blog_stats]

Examples and Usage

Basic example – A simple usage of the ‘sbs_blog_stats’ shortcode to display blog statistics on your site.

[sbs_blog_stats /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_blog_stats] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_blog_stats','sbs_blog_stats');

Shortcode PHP function:

function sbs_blog_stats() {
	global $sbs_options;

	$count_posts = wp_count_posts();
	$number_posts = $count_posts->publish;

	$count_pages = wp_count_posts('page');
	$number_pages = $count_pages->publish;

	$count_drafts = wp_count_posts();
	$number_drafts = $count_drafts->draft;

	$count_comments = wp_count_comments();
	$number_comments = $count_comments->total_comments;

	$count_moderated = wp_count_comments();
	$number_moderated = $count_moderated->moderated;

	$count_approved = wp_count_comments();
	$number_approved = $count_approved->approved;

	$count_users = count_users();
	$number_users = $count_users['total_users'];

	$cats = wp_list_categories('title_li=&style=none&echo=0');
	$cats_parts = explode('<br />', $cats);
	$cats_count = count($cats_parts) - 1;
	$number_cats = $cats_count;

	$number_tags = wp_count_terms('post_tag');
	$number_words = sbs_word_count_all(false);
	
	$sbs_stats  = '<ul id="sbs-stats">';
	$sbs_stats .= '<li><span>' . $number_posts     . '</span> ' . esc_html__('posts', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_pages     . '</span> ' . esc_html__('pages', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_drafts    . '</span> ' . esc_html__('drafts', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_words     . '</span> ' . esc_html__('words', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_comments  . '</span> ' . esc_html__('total comments', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_moderated . '</span> ' . esc_html__('comments in queue', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_approved  . '</span> ' . esc_html__('comments approved', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_users     . '</span> ' . esc_html__('registered users', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_cats      . '</span> ' . esc_html__('categories', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '<li><span>' . $number_tags      . '</span> ' . esc_html__('tags', 'simple-blog-stats') . '</li>';
	$sbs_stats .= '</ul>';

	return $sbs_options['blog_stats_before'] . $sbs_stats . $sbs_options['blog_stats_after'];
}

Code file location:

simple-blog-stats/simple-blog-stats/simple-blog-stats.php

Simple Blog Stats [sbs_logged_users] Shortcode

The Simple Blog Stats shortcode ‘sbs_logged_users’ displays the number of currently logged users. It retrieves this data from a transient ‘online_status’. This shortcode also allows customization. It uses ‘logged_users_before’ and ‘logged_users_after’ options from the plugin’s settings to add content before and after the count. If no users are logged in, it displays a customizable message.

Shortcode: [sbs_logged_users]

Examples and Usage

Basic example – Display the number of currently logged-in users on your site.

[sbs_logged_users /]

PHP Function Code

In case you have difficulties debugging what causing issues with [sbs_logged_users] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('sbs_logged_users', 'sbs_logged_users_shortcode');

Shortcode PHP function:

function sbs_logged_users_shortcode() {
	
	global $sbs_options;
	
	$before = isset($sbs_options['logged_users_before']) ? $sbs_options['logged_users_before'] : '';
	
	$after = isset($sbs_options['logged_users_after']) ? $sbs_options['logged_users_after'] : '';
	
	$logged_in_users = get_transient('online_status');
	
	$count_zero = __('Currently no users logged in.', 'simple-blog-stats');
	
	$count_zero = apply_filters('sbs_no_logged_users', $count_zero);
	
	$count = !empty($logged_in_users) ? count($logged_in_users) : $count_zero;
	
	return $before . $count . $after;
	
}

Code file location:

simple-blog-stats/simple-blog-stats/stats-functions.php

Conclusion

Now that you’ve learned how to embed the Simple Blog Stats Plugin shortcodes, 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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *