Get the Official WPSaviour App Now!


Post Reading Duration WordPress Shortcode

I observed a few days previously a website online providing a little bit of more information on the very best of the net web page: the estimated time to be taught the net web page. I found it humorous and wanted to create one factor comparable using a  WordPress shortcode.  Lets see learn how to create Put up Finding out Interval WordPress Shortcode.

That’s pretty easy to do, so let’s start!

Step 1: Create A Plugin

As common we’ll create a plugin, so, create a model new folder beneath wp-content/plugins, and title it “post-reading-duration-shortcode“, open it, and create a file known as “post-reading-duration-shortcode.php“. Open the file and paste this code:

Plugin Determine: Put up Finding out Interval Shortcode
Plugin URL:
Description: Present the estimated time to be taught the submit
Mannequin: 0.1
Author: Remi Corson
Author URI:
Contributors: corsonr
Textual content material Space: rc_prds
Space Path: languages

While you save the file that you must see the plugin itemizing on the plugins net web page.

Step 2: Create The Shortcode

Principally the plugin will current a simple shortcode with out attribute. The textual content material shall be encapsulated between the shortcode tags themselves.The shortcode will present on the frontend the time company must be taught the submit. To create the Put up Finding out Interval WordPress Shortcode, use this code:

 * Register the shortcode
 * @entry      public
 * @since       1.0 
 * @return      void
function rc_prds_add_reading_duration_shortcode( $atts, $content material materials = null ) {
	return '<span class="reading_duration">'.$content material materials.'</span>';

add_shortcode("reading_duration", "rc_prds_add_reading_duration_shortcode");

It could actually prevent the file. Within the occasion you add to a submit or an internet web page the shortcode [reading_duration]Estimated finding out time: XX[/reading_duration], it should current “Estimated finding out time: XX” encapsulated proper right into a “span” tag.

The next step is to change the shortcode to indicate the true estimated time needed to be taught the net web page or the submit, in step with a predefined number of phrases be taught per minute.

To take motion, now now we have to make the shortcode code evolve. We have now to get the submit ID (to retrieve the number of phrases inside the submit using str_word_count()), and now we have to stipulate quite a lot of phrases people usually be taught in a single minute. Let’s say 200 phrases per minute.

 * Register the shortcode
 * @entry      public
 * @since       1.0 
 * @return      void
function rc_prds_add_reading_duration_shortcode( $atts, $content material materials = null ) {

	worldwide $submit;
	// Get Put up ID
	$post_id = $submit->ID;
	// Phrases be taught per minute (you can set your private price)
	$words_per_minute = 200;
	// Get estimated time
	$estimated_reading_time = rc_prds_get_post_reading_time( $post_id, $words_per_minute );
	return '<span class="reading_duration">'.$content material materials.' '.$estimated_reading_time.'</span>';

add_shortcode("reading_duration", "rc_prds_add_reading_duration_shortcode");

Step 3: The Counter Function

As you can see there’s an undefined function: rc_prds_get_post_reading_time(). That’s the function that will return the estimated time you may need to be taught the submit. Let’s create it:

 * Get submit reding time
 * @entry      public
 * @since       1.0 
 * @return      void
function rc_prds_get_post_reading_time( $post_id, $words_per_minute ){

	// Get submit's phrases
	$content material materials     = get_the_content( $post_id );
	$count_words = str_word_count( strip_tags( $content material materials ) );
	// Get Estimated time
	$minutes = flooring( $count_words / $words_per_minute);
	$seconds = flooring( ($count_words / ($words_per_minute / 60) ) - ( $minutes * 60 ) );
	// If decrease than a minute
	if( $minutes < 1 ) {
		$estimated_time = __( 'Decrease than a minute', 'rc_prds' );
	// If larger than a minute
	if( $minutes >= 1 ) {
		if( $seconds > 0 ) {
			$estimated_time = $minutes.__( 'min', 'rc_prds' ).' '.$seconds.__( 'sec', 'rc_prds' );
		} else {
			$estimated_time = $minutes.__( 'min', 'rc_prds' );
	return $estimated_time;

Principally, the function counts the number of phrases in “get_the_content()”, then rely number of minutes and seconds needed to be taught the submit. The ultimate half is barely a simple technique to indicate the correct message counting on the values of $minutes and $seconds.

More Blogs

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogTutorials

Use Gutenberg like a pro with keyboard shortcuts

Beginner’s GuideBlogSecurityWordPress Security

A Simple Explanation of SSL Certificate Errors & How to Fix Them

Beginner’s GuideBest WordPress PluginsBlog

9 Best A/B Split Testing Plugins for WordPress

Best WordPress PluginsBlog

7 Best Plugins for Tracking User Activity in WordPress

Sign up for our Newsletter and
stay informed

Fear Of Missing Out?

Sign up with your email address to receive WordPress tips and updates
Terms and Conditions apply
Click Me