wordpress, 未分类

How to Add Reading Time and Word Count to WordPress Posts

✅ Accurate Reading Time & Word Count for Chinese WordPress Sites (No Plugin Needed!)

Many premium WordPress themes and plugins offer “Reading Time” features, but they often miscalculate statistics for Chinese content due to fundamental differences in how characters are counted between languages. This guide shows you how to accurately display reading time and word count for Chinese articles in WordPress—without any plugins.


🛠 Step 1: Add the Reading Time Function to functions.php

Open your theme’s functions.php file. Add the following code before the closing ?> tag (or at the end if no tag exists):

/**
 * Adds accurate reading time & word count for Chinese content in WordPress
 * Source: https://www.wolzq.com/blog/10786
 */
function zzb_reading_time() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // Words per minute. Use 300 for Chinese content (adjustable)
    $clean_content = strip_shortcodes($content);
    $clean_content = strip_tags($clean_content);
    $word_count = mb_strlen($clean_content, 'UTF-8'); // Correctly counts Chinese characters
    $time = ceil($word_count / $wpm);
    return 'Words: ' . $word_count . '    |    Reading Time: ' . $time . ' min';
}

📌 Critical Note:
This function requires the mbstring PHP extension. Most hosting providers enable it by default. Verify under PHP Info → mbstring section.

🛡️ Pro Tip: Use plugins like WPCode Pro to manage code snippets safely—avoid editing functions.php directly!


🔌 Step 2: Display the Statistics in Your Theme

Place this code where you want the stats to appear (e.g., in single.phpcontent-single.php, or archive templates):

<?php echo zzb_reading_time(); ?>

Output Example:
Words: 866 | Reading Time: 3 min


⚙️ Step 3 (Optional): Show Only Reading Time

Modify the return line in the function to:

return '<span id="read-time">Reading Time: ' . $time . ' min</span>';

🎨 Step 4: Style for Visual Appeal

Add CSS to your theme’s style.css or Appearance → Customize → Additional CSS:

/* Reading Stats Styling */
#read-count, #read-time {
    font-size: 14px;
    color: #707070;
    display: inline-block;
    margin: 10px 0;
    padding: 4px 8px;
    background: #f8f9fa;
    border-radius: 4px;
}

💡 Why This Matters for Chinese Sites

  • Accuracy: Uses mb_strlen() for correct Chinese character counting.

  • Speed: Plugin-free = better performance.

  • UX: Readers appreciate transparency about content length.

  • SEO: Clean code + engagement signals = ranking boost.


🚀 Get Started Today!

Implement this lightweight solution to:

  • Enhance user experience for Chinese readers

  • Maintain site performance

  • Add valuable content metrics

  • Stand out from plugin-dependent sites

→ No plugins. No inaccuracies. Just results.

Optimize your WordPress content strategy now!