Reading Time WP Shortcode

Below, you’ll find a detailed guide on how to add the Reading Time WP 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 Reading Time WP Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Reading Time WP Plugin and the shortcodes it provides:

Plugin Icon
Reading Time WP

"Reading Time WP is a plugin that calculates and displays the estimated time a reader would need to go through your WordPress content, enhancing user experience."

★★★★☆ (18) Active Installs: 20000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [rt_reading_time]

Reading Time WP [rt_reading_time] Shortcode

The ‘rt_reading_time’ shortcode is designed to estimate and display the reading time for a specific WordPress post. It takes into account attributes such as label, postfix, postfix_singular, and post_id. The shortcode retrieves the options for reading time, calculates it for the selected post, and adds the appropriate postfix. The result is neatly wrapped in HTML span tags for easy styling.

Shortcode: [rt_reading_time]

Parameters

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

  • label – a tag or name you want to assign to the reading time.
  • postfix – additional text added after the reading time.
  • postfix_singular – text added after the reading time when it’s one minute.
  • post_id – the specific ID of the post you want to calculate the reading time for.

Examples and Usage

Basic example – Display the reading time of a post using the shortcode. The ‘rt_reading_time’ shortcode does not require any parameters to work. By default, it will display the reading time for the current post.

[rt_reading_time /]

Advanced examples

Display the reading time with a custom label and postfix. The ‘label’ attribute allows you to customize the text that appears before the reading time. The ‘postfix’ attribute allows you to customize the text that appears after the reading time. In this example, the reading time will be displayed as “Reading Duration: X minutes approx.”

[rt_reading_time label="Reading Duration:" postfix="minutes approx." /]

Display the reading time of a specific post. The ‘post_id’ attribute allows you to specify the ID of the post you want to calculate the reading time for. In this example, the reading time for the post with ID 123 will be displayed.

[rt_reading_time post_id=123 /]

Combining all attributes. In this example, the reading time for the post with ID 123 will be displayed with a custom label and postfix.

[rt_reading_time label="Estimated Time:" postfix="minutes approx." post_id=123 /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'rt_reading_time', array( $this, 'rt_reading_time' ) );

Shortcode PHP function:

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

		$atts = shortcode_atts(
			array(
				'label'            => '',
				'postfix'          => '',
				'postfix_singular' => '',
				'post_id'          => '',
			),
			$atts,
			'rt_reading_time'
		);

		$rt_reading_time_options = get_option( 'rt_reading_time_options' );

		// If post_id attribute was specified that exists, then use that to calculate read time, else use the current post ID.
		$rt_post = $atts['post_id'] && ( get_post_status( $atts['post_id'] ) ) ? $atts['post_id'] : get_the_ID();

		$this->rt_calculate_reading_time( $rt_post, $rt_reading_time_options );

		$calculated_postfix = $this->rt_add_postfix( $this->reading_time, $atts['postfix_singular'], $atts['postfix'] );

		return '<span class="span-reading-time rt-reading-time"><span class="rt-label rt-prefix">' . wp_kses( $atts['label'], $this->rtwp_kses ) . '</span> <span class="rt-time"> ' . esc_html( $this->reading_time ) . '</span> <span class="rt-label rt-postfix">' . wp_kses( $calculated_postfix, $this->rtwp_kses ) . '</span></span>';
	}

Code file location:

reading-time-wp/reading-time-wp/rt-reading-time.php

Conclusion

Now that you’ve learned how to embed the Reading Time WP 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.

Comments

Leave a Reply

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