Make WordPress Core

Opened 14 months ago

Last modified 9 months ago

#57987 new enhancement

Introduce new `wp_word_count` function

Reported by: wildworks's profile wildworks Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Editor Keywords: has-patch has-unit-tests
Focuses: Cc:


In the WordPress core, there is a JavaScript-based WordCounter( class to count the number of characters appropriately based on language type. My understanding is that this logic is incorporated into the @wordpress/wordcount( package in Gutenberg. This utility is used in the document panel of the block editor to calculate the number of characters.

Also, in Gutenberg PR(, a new "Post Time To Read" block was added. This block shows minutes required to finish reading the post. This block uses the wordcount package to calculate the number of characters in the content from which the time is calculated. However, the exact same PHP-based logic was needed to render this block server-side on the front-end side. Therefore, we added a new wp_word_count() PHP function at the same time we added this block.

Since this block is experimental, the block and this function will not be bundled into the core. However, given the possibility that this block may become less experimental and be incorporated into the core in the future, and given the recent addition of the wp_get_word_count_type() function, we may consider implementing this function in the core.

This PHP function is a direct transfer of the logic of the JavaScript-based WordCounter.count. Unit tests have also been implemented.

Change History (4)

#1 @sabernhardt
13 months ago

  • Component changed from General to Editor

This ticket was mentioned in PR #4430 on WordPress/wordpress-develop by @wildworks.

13 months ago

  • Keywords has-patch has-unit-tests added

#3 @wildworks
13 months ago

I have attached the patch. This patch includes the function itself and unit tests.

The unit test is based on this Qunit test. The test cases and expected results are exactly the same as for QUnit.:

The function is based on this JS file. While there are differences in the way regular expressions are written due to language, I believe the logic is the same.:

If there is anything I am missing, please let me know.

#4 @wildworks
9 months ago

The Post Time To Read block that exists on the Gutenberg plugin depends on this function. I'm hoping this block will ship in WP6.4 and would be happy to move this ticket forward.

Note: See TracTickets for help on using tickets.