PHP snippet · Content & Text

Reading time shortcode

A shortcode that prints an estimated read time from the post content. Words per minute and the wording after the number are both attributes.

About this php snippet

Drop the shortcode into a post, a template, or any page builder text widget and it prints an estimate such as 4 min read, counted from the post content at the moment the page is built.

200 words per minute is the usual figure for online reading. Raise it for a technical audience who skim, lower it for long form that people actually sit with.

Usage

  • [emcp_reading_time]
  • [emcp_reading_time wpm="240" suffix="minute read"]
  • [emcp_reading_time id="42"] to read a different post

What it does

  • Counts the words in the post content with markup and other shortcodes stripped out first, so a page full of embeds is not counted as reading
  • Rounds up with a floor of one minute, so a short post never reads as zero
  • Falls back to the current post when no id is given, and prints nothing when there is no content to count

Good to know

  • Word counting uses a whitespace split rather than the built-in PHP word counter, which only understands single-byte letters and would badly undercount any language with accents or a non-Latin script
  • Elementor keeps its layout in post meta rather than post content, so on an Elementor page there is usually nothing for it to count. Classic and Gutenberg posts are fully covered.
  • Output is escaped, so it is safe to place anywhere
  • Works in Elementor text widgets, Gutenberg, sidebar widgets, and classic templates

More php snippets