WordPress Shortcode for Post Reading Time

- Advertisement -

Creating a WordPress Plugin to Display Estimated Reading Time

Have you ever come across a website that displays the estimated time to read a page? It’s a handy feature that can help readers decide whether they have enough time to read an article or not. In this article, we’ll show you how to create a WordPress plugin that displays the estimated reading time for your posts.

- Advertisement -

Step 1: Create a Plugin

The first step is to create a plugin. Create a new folder under wp-content/plugins and name it “post-reading-duration-shortcode.” Open the folder and create a file called “post-reading-duration-shortcode.php.” Paste the following code into the file:

- Advertisement -

“`

- Advertisement -

/*

Plugin Name: Post Reading Duration Shortcode

Plugin URL: http://remicorson.com/

Description: Display the estimated time to read the post

Version: 0.1

Author: Remi Corson

Author URI: http://remicorson.com

Contributors: corsonr

Text Domain: rc_prds

Domain Path: languages

*/

“`

Once you save the file, you should see the plugin listed on the plugins page.

Step 2: Create the Shortcode

The plugin will provide a simple shortcode without attributes. The text will be encapsulated between the shortcode tags themselves. The shortcode will display on the frontend the time visitors need to read the post. To create the shortcode, use this code:

“`

/**

* Register the shortcode

*

* @access public

* @since 1.0

* @return void

*/

function rc_prds_add_reading_duration_shortcode( $atts, $content = null ) {

return ‘‘.$content.’‘;

}

add_shortcode(“reading_duration”, “rc_prds_add_reading_duration_shortcode”);

“`

If you add to a post or a page the shortcode `[reading_duration]Estimated reading time: XX[/reading_duration]`, it should show “Estimated reading time: XX” encapsulated into a “span” tag.

The next step is to modify the shortcode to display the real estimated time needed to read the page or the post, according to a predefined number of words read per minute. To do so, we have to make the shortcode code evolve. We need to get the post ID (to retrieve the number of words in the post using `str_word_count()`), and we need to define a number of words people usually read in one minute. Let’s say 200 words per minute.

“`

/**

* Register the shortcode

*

* @access public

* @since 1.0

* @return void

*/

function rc_prds_add_reading_duration_shortcode( $atts, $content = null ) {

global $post;

// Get Post ID

$post_id = $post->ID;

// Words read per minute (you can set your own value)

$words_per_minute = 200;

// Get estimated time

$estimated_reading_time = rc_prds_get_post_reading_time( $post_id, $words_per_minute );

return ‘‘.$content.’ ‘.$estimated_reading_time.’‘;

}

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()`. This is the function that will return the estimated time you need to read the post. Let’s create it:

“`

/**

* Get post reading time

*

* @access public

* @since 1.0

* @return void

*/

function rc_prds_get_post_reading_time( $post_id, $words_per_minute ){

// Get post’s words

$content = get_the_content( $post_id );

$count_words = str_word_count( strip_tags( $content ) );

// Get Estimated time

$minutes = floor( $count_words / $words_per_minute);

$seconds = floor( ($count_words / ($words_per_minute / 60) ) – ( $minutes * 60 ) );

// If less than a minute

if( $minutes < 1 ) {

$estimated_time = __( ‘Less than a minute’, ‘rc_prds’ );

}

// If more 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;

}

“`

The function counts the number of words in `get_the_content()`, then counts the number of minutes and seconds needed to read the post. The last part is just a simple way to display the correct message depending on the values of `$minutes` and `$seconds`.

You can now fully use the shortcode by simply adding:

“`

[reading_duration]Estimated Reading Time:[/reading_duration]

“`

In conclusion, creating a WordPress plugin to display estimated reading time is a great way to enhance user experience on your website. It’s a simple feature that can make a big difference in how readers engage with your content. With the steps outlined above, you can easily create your own plugin and add this feature to your website.

- Advertisement -

Stay in Touch

spot_img

Related Articles