How to add word count and estimated reading time of articles in WordPress?

somenew mediaThe article on the website begins with a word count and expected reading time for the article.

  • Chen WeiliangI think these two small data are quite humanized and very beneficial to users.
  • In this way, readers can estimate the length of the article and their approximate reading time before reading.
  • Today we will discuss how toWordPressAdded article count statistics and estimated reading time.

How to add word count and estimated reading time of articles in WordPress?

XNUMX. Add word count code for WordPress articles

Add the following code to the last few functions.php files in your theme ?> before ▼

//字数统计
function count_words ($text) {
global $post;
if ( '' == $text ) {
$text = $post->post_content;
if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '本文《' . get_the_title() .'》共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') . '个字';
return $output;
}
  • After testing, the above code stats have no problem in Chinese and English;
  • And the exact same number of words are counted in Microsoft Word.

XNUMX. Estimated reading time for WordPress

Add the following code to the last few functions.php files in your theme ?>

After saving, you can automatically display "Estimated reading time x minutes" at the beginning of your WordPress post content▼

function lmsim_read_time($content){
$text = trim(strip_tags( get_the_content()));
$text_num = mb_strlen($text, 'UTF-8');
$read_time = ceil($text_num/400);
$content = '<div class="read-time">系统预计阅读时间 <span>' . $read_time . '</span> 分钟</div>' . $content;
return $content;
}
add_filter ( 'the_content', 'lmsim_read_time');
  • The value of line 4 in the above code is 400, based on Baidu's "average reading speed of ordinary people (300~500) words/minute".
  • If you think 400 is too slow, you can modify it yourself.
  • You need a custom style.You can style .read-time in custom css.

After the test, it is found that the number of words in the above code statistics has some errors, these errors exceed the actual errors

  • The number of words in A website stats is 290 characters, and the stats in Word are the same.
  • With B site the word count ($text_num) is 12 more than the actual number.
  • This expected reading time can only appear at the beginning of the article, soChen WeiliangDecided to combine these 2 codes for optimization.

XNUMX. Optimize the expected reading time

Add the following code to the last few functions.php files in your theme ?> before ▼

//字数和预计阅读时间统计
function count_words_read_time () {
global $post;
$text_num = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8');
$read_time = ceil($text_num/400);
$output .= '本文《' . get_the_title() .'》共' . $text_num . '个字,系统预计阅读时间或需' . $read_time . '分钟。';
return $output;
}
  • Of these, 400 or higher is the read speed and can be modified.
  • If you only need to output reading time or article word count, you only need to modify and delete some of the lines in line 6.
  • Please do it yourself DIY.

Then, add the call statistics code to the appropriate location in the single.php file.

<?php echo count_words_read_time(); ?>

XNUMX. Comparison before and after Estimated Reading Timecode Optimization

OnChen WeiliangAfter the test, when the word count is less than or equal to 400, i.e. when the expected reading time is less than or equal to 1 minute.

However, if it exceeds 400, it will be biased.

  • For example, if the above 290 characters were pasted 3 times to reach 1160 characters, the estimated reading time for point 2 would be 4 minutes,
  • Code optimized for point 3 will be 3 minutes.
  • So from a numerical point of view, it is more accurate to optimize the estimated read time of code statistics.

(ceil() function)What is it?

ceil() The function rounds up to the nearest integer.

This means to return the next integer not less than x.

If x has a fractional part, thenceil() The returned type is stillfloat,becausefloatrange is usually greater thaninteger.

example

  • ceil(0.60), output 1;
  • ceil(0.4) , output 1;
  • ceil(5), output 5;
  • ceil(5.1), output 6;
  • ceil (-5.1), output -5;
  • ceil(-5.9), output -5;

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How to add article word count and estimated reading time in WordPress? , to help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-1107.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top