{"id":45450,"date":"2022-05-16T21:49:46","date_gmt":"2022-05-16T21:49:46","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45450"},"modified":"2026-10-10T17:44:53","modified_gmt":"2026-10-10T17:44:53","slug":"post-reading-time-shortcode","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/post-reading-time-shortcode\/","title":{"rendered":"WordPress Post Reading Time Shortcode"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A WordPress post reading time shortcode shows readers how long an article takes, such as &#8220;4 min read&#8221;, next to the title or at the top of the post. <code>&#91;reading_time&#93;<\/code> estimates it from the post&#8217;s word count and works inside the post content, in a Shortcode block in a block theme template and in PHP templates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * &#91;reading_time&#93; shortcode: estimated reading time of a post.\n * Usage: &#91;reading_time&#93;, &#91;reading_time wpm=\"250\"&#93;, &#91;reading_time post_id=\"42\" label=\"false\"&#93;\n *\/\n\nadd_action( 'init', 'dpc_reading_time_register' );\n\nfunction dpc_reading_time_register() {\n\tadd_shortcode( 'reading_time', 'dpc_reading_time_shortcode' );\n}\n\n\/**\n * Reading time in whole minutes, from the raw post content.\n * Returns 0 for a post without words.\n *\/\nfunction dpc_get_reading_time( $post = null, $words_per_minute = 200 ) {\n\t$post = get_post( $post );\n\tif ( ! $post ) {\n\t\treturn 0;\n\t}\n\n\t\/\/ Raw content: no the_content filter, so no recursion when the shortcode sits in the post itself.\n\t$text = strip_shortcodes( $post-&gt;post_content );\n\t$text = wp_strip_all_tags( $text ); \/\/ Also removes the &lt;!-- wp:... --&gt; block comments.\n\t$text = html_entity_decode( $text, ENT_QUOTES, get_bloginfo( 'charset' ) );\n\n\t$words = preg_split( '\/\\s+\/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY );\n\t$count = is_array( $words ) ? count( $words ) : 0;\n\n\treturn $count &gt; 0 ? (int) ceil( $count \/ max( 1, (int) $words_per_minute ) ) : 0;\n}\n\nfunction dpc_reading_time_shortcode( $atts ) {\n\t$atts = shortcode_atts(\n\t\tarray(\n\t\t\t'wpm'     =&gt; 200,\n\t\t\t'post_id' =&gt; 0,\n\t\t\t'label'   =&gt; 'true',\n\t\t),\n\t\t$atts,\n\t\t'reading_time'\n\t);\n\n\t$wpm = absint( $atts&#91;'wpm'&#93; );\n\tif ( $wpm &lt; 50 || $wpm &gt; 1000 ) {\n\t\t$wpm = 200;\n\t}\n\n\t$post = absint( $atts&#91;'post_id'&#93; ) ? get_post( absint( $atts&#91;'post_id'&#93; ) ) : get_post();\n\tif ( ! $post || ( ! is_post_publicly_viewable( $post ) &amp;&amp; ! current_user_can( 'read_post', $post-&gt;ID ) ) ) {\n\t\treturn '';\n\t}\n\n\t$minutes = dpc_get_reading_time( $post, $wpm );\n\tif ( 0 === $minutes ) {\n\t\treturn '';\n\t}\n\n\t$text = wp_validate_boolean( $atts&#91;'label'&#93; )\n\t\t\/* translators: %d: number of minutes. *\/\n\t\t? sprintf( _n( '%d min read', '%d min read', $minutes, 'dpc' ), $minutes )\n\t\t: (string) $minutes;\n\n\treturn '&lt;span class=\"dpc-reading-time\"&gt;' . esc_html( $text ) . '&lt;\/span&gt;';\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Use it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&#91;reading_time&#93;<\/code> prints <code>&lt;span class=\"dpc-reading-time\"&gt;4 min read&lt;\/span&gt;<\/code> for the current post.<\/li>\n\n\n<li><code>wpm<\/code> sets words per minute (50 to 1000, default 200). Anything else falls back to 200.<\/li>\n\n\n<li><code>label=\"false\"<\/code> prints only the number, for your own wording around it.<\/li>\n\n\n<li><code>post_id=\"42\"<\/code> measures another post, for example in a sidebar or a related-posts list.<\/li>\n\n\n<li>In a PHP template: <code>echo do_shortcode( '&#91;reading_time&#93;' );<\/code>, or <code>dpc_get_reading_time()<\/code> for the bare number.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The words come from the raw <code>post_content<\/code>. The shortcode never runs the <code>the_content<\/code> filter, which would render the shortcode again inside itself and loop until PHP runs out of memory, which is the bug in the older version of this post.<\/li>\n\n\n<li><code>strip_shortcodes()<\/code> removes shortcodes, <code>wp_strip_all_tags()<\/code> removes HTML and the <code>&lt;!-- wp:... --&gt;<\/code> block comments, so markup and block settings are not counted as words.<\/li>\n\n\n<li>Words are split on whitespace with a Unicode-aware pattern, so accented words count correctly, unlike <code>str_word_count()<\/code>.<\/li>\n\n\n<li>The result is rounded up, so a short post shows 1 minute, and a post with no words prints nothing.<\/li>\n\n\n<li>Attributes go through <code>shortcode_atts()<\/code>, <code>absint()<\/code> and <code>wp_validate_boolean()<\/code>, the output through <code>esc_html()<\/code>. A private or draft post passed in <code>post_id<\/code> only shows for users who can read it.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The older version also had a <code>gt<\/code> attribute that always ran because the string &#8220;false&#8221; is truthy; it is no longer needed and is ignored if old shortcodes still pass it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty. Everywhere registers the shortcode on every request, including the REST requests the block editor uses to preview a Shortcode block; <strong>Conditions<\/strong> would only register it on matching front-end pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Different wording: change <code>'%d min read'<\/code> (both strings in <code>_n()<\/code>), for example to <code>'%d minute read'<\/code> and <code>'%d minutes read'<\/code>.<\/li>\n\n\n<li>Automatically above every post, without a shortcode: hook a filter on <code>the_content<\/code> that prepends <code>do_shortcode( '&#91;reading_time&#93;' )<\/code> when <code>is_singular( 'post' ) &amp;&amp; in_the_loop()<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Already using <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/read-time-without-the-plugin\/\">Read time without the plugin<\/a>? This version adds a template helper and attribute validation. For another small shortcode, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/fluid-type-preview-shortcode\/\">Fluid Type Preview shortcode<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Text that blocks render dynamically (query loops, reusable patterns, synced patterns) is not in <code>post_content<\/code> and is not counted.<\/li>\n\n\n<li>Chinese, Japanese and Thai don&#8217;t separate words with spaces, so the count is far too low; count characters with <code>mb_strlen()<\/code> and divide by about 500 instead.<\/li>\n\n\n<li>The tag <code>reading_time<\/code> has no prefix so existing shortcodes keep working. If another plugin registers the same tag, the one added last wins; rename it in <code>add_shortcode()<\/code> and in <code>shortcode_atts()<\/code> if needed.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>A WordPress reading time shortcode that counts the raw post words, skips blocks and shortcodes, and works in content and templates.<\/p>\n","protected":false},"author":18,"featured_media":46679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress Post Reading Time Shortcode - Code DPlugins","description":"A WordPress reading time shortcode that counts the raw post words, skips blocks and shortcodes, and works in content and templates."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-45450","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=45450"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45450\/revisions"}],"predecessor-version":[{"id":46678,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45450\/revisions\/46678"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46679"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=45450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45450"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=45450"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}