{"id":45842,"date":"2022-07-23T18:53:07","date_gmt":"2022-07-23T18:53:07","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45842"},"modified":"2026-10-10T19:59:54","modified_gmt":"2026-10-10T19:59:54","slug":"read-time-without-the-plugin","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/read-time-without-the-plugin\/","title":{"rendered":"Show Reading Time in WordPress Without a Plugin"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Medium shows &#8220;4 min read&#8221; at the top of every article, and readers like to know how long a post will take before they start. You can show the estimated reading time in WordPress without a plugin: this snippet adds a small template function that counts the words of a post at about 200 words per minute, the figure Medium uses, and adds a little time for each image, as Medium does.<\/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 * Estimated reading time for theme templates: 200 words per minute plus time for images, like Medium.\n *\n * Template usage: dpc_the_read_time();\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_read_time_config(): array {\n\treturn array(\n\t\t'words_per_minute' =&gt; 200,\n\t\t\/\/ Medium adds 12 s for the first image, 11 s for the second, down to 3 s. false = count words only.\n\t\t'count_images'     =&gt; true,\n\t);\n}\n\/\/ ===== End of config =====\n\n\/**\n * Reading time of a post in minutes (at least 1 for a post with any content).\n *\/\nfunction dpc_read_time_minutes( $post = null ): int {\n\t$post = get_post( $post );\n\tif ( ! $post ) {\n\t\treturn 0;\n\t}\n\n\t$config  = dpc_read_time_config();\n\t$content = strip_shortcodes( (string) $post-&gt;post_content );\n\n\t$text  = html_entity_decode( wp_strip_all_tags( $content ), ENT_QUOTES, get_bloginfo( 'charset' ) );\n\t$words = preg_split( '\/\\s+\/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY );\n\t$words = is_array( $words ) ? count( $words ) : 0;\n\n\t$seconds = $words \/ max( 1, (int) $config&#91;'words_per_minute'&#93; ) * 60;\n\n\tif ( ! empty( $config&#91;'count_images'&#93; ) ) {\n\t\t$images = preg_match_all( '\/&lt;img\\b\/i', $content );\n\t\tfor ( $i = 0; $i &lt; $images; $i++ ) {\n\t\t\t$seconds += max( 3, 12 - $i );\n\t\t}\n\t}\n\n\treturn $seconds &gt; 0 ? (int) ceil( $seconds \/ 60 ) : 0;\n}\n\n\/**\n * Reading time as text, e.g. \"3 minutes\". Empty string for a post without content.\n *\/\nfunction dpc_read_time( $post = null ): string {\n\t$minutes = dpc_read_time_minutes( $post );\n\tif ( 0 === $minutes ) {\n\t\treturn '';\n\t}\n\n\t\/* translators: %d: number of minutes. *\/\n\treturn sprintf( _n( '%d minute', '%d minutes', $minutes, 'dpc' ), $minutes );\n}\n\n\/**\n * Print the reading time, escaped, with an optional label.\n *\/\nfunction dpc_the_read_time( $post = null, string $label = '' ): void {\n\t$text = dpc_read_time( $post );\n\tif ( '' === $text ) {\n\t\treturn;\n\t}\n\n\tif ( '' === $label ) {\n\t\t$label = __( 'Read time:', 'dpc' );\n\t}\n\n\tprintf( '&lt;p class=\"dpc-read-time\"&gt;%s %s&lt;\/p&gt;', esc_html( $label ), esc_html( $text ) );\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then call it in your theme template where the reading time should appear, for example in <code>single.php<\/code> below the title:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php dpc_the_read_time(); ?&gt;\n\n&lt;?php \/\/ Or build your own markup: ?&gt;\n&lt;span class=\"read-time\"&gt;&lt;?php echo esc_html( dpc_read_time() ); ?&gt;&lt;\/span&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dpc_read_time_minutes()<\/code> reads the raw <code>post_content<\/code> of the post, removes shortcodes, HTML tags and the <code>&lt;!-- wp:... --&gt;<\/code> block comments, and counts words on whitespace. The pattern is Unicode-aware, so words with accents count once, unlike <code>str_word_count()<\/code>.<\/li>\n\n\n<li>With <code>count_images<\/code> on, each <code>&lt;img&gt;<\/code> adds time the way Medium does it: 12 seconds for the first, one second less for each next image, never less than 3 seconds.<\/li>\n\n\n<li>The result is rounded up to whole minutes, so a short post shows 1 minute. A post without content returns 0 and prints nothing.<\/li>\n\n\n<li><code>dpc_read_time()<\/code> returns translatable text such as &#8220;3 minutes&#8221;. <code>dpc_the_read_time()<\/code> prints it with a label, both escaped with <code>esc_html()<\/code>.<\/li>\n\n\n<li>Without an argument the functions use the current post, so they work inside The Loop. Pass a post ID or object anywhere else.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post read <code>$post-&gt;ID<\/code> without <code>$post<\/code> existing in the function, which is a warning on PHP 8 and the reason it only ever worked by accident. Its generic name <code>reading_time()<\/code> could also collide with a theme or plugin.<\/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 snippet (not the template part) into the PHP editor, edit the config and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Everywhere<\/strong> loads the functions the same way <code>functions.php<\/code> does, before any template runs. The template part goes into your theme or child theme file. Wrap the call in <code>if ( function_exists( 'dpc_the_read_time' ) )<\/code> if the code block might ever be disabled.<\/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>Slower or faster readers: change <code>words_per_minute<\/code>. Technical posts read closer to 150.<\/li>\n\n\n<li>Words only: set <code>'count_images' =&gt; false<\/code>.<\/li>\n\n\n<li>Block themes have no PHP templates. Use the <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/post-reading-time-shortcode\/\">Post reading time shortcode<\/a> in a Shortcode block instead.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Content that blocks render on their own, such as Query Loops, synced patterns and gallery shortcodes, is not in <code>post_content<\/code> and is not counted.<\/li>\n\n\n<li>Chinese, Japanese and Thai do not 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<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Show the estimated reading time of a WordPress post in your theme without a plugin: 200 words per minute plus time for images.<\/p>\n","protected":false},"author":1,"featured_media":46750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Show Reading Time in WordPress Without a Plugin","description":"Show the estimated reading time of a WordPress post in your theme without a plugin: 200 words per minute plus time for images."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[36],"tags":[],"dpc_library":[204],"class_list":["post-45842","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerce"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45842","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=45842"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45842\/revisions"}],"predecessor-version":[{"id":46749,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45842\/revisions\/46749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46750"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45842"}],"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=45842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45842"},{"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=45842"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}