[wp-trac] [WordPress Trac] #57987: Introduce new `wp_word_count` function
WordPress Trac
noreply at wordpress.org
Sat Nov 15 01:41:27 UTC 2025
#57987: Introduce new `wp_word_count` function
-------------------------------------------------+-------------------------
Reporter: wildworks | Owner: pbearne
Type: enhancement | Status: closed
Priority: normal | Milestone:
Component: Editor | Version:
Severity: normal | Resolution: maybelater
Keywords: has-patch has-unit-tests needs- | Focuses:
testing |
-------------------------------------------------+-------------------------
Description changed by dmsnell:
Old description:
> In the WordPress core, there is a JavaScript-based
> `WordCounter`(https://core.trac.wordpress.org/browser/trunk/src/js/_enqueues/wp/utils
> /word-count.js) class to count the number of characters appropriately
> based on language type. My understanding is that this logic is
> incorporated into the
> `@wordpress/wordcount`(https://github.com/WordPress/gutenberg/tree/trunk/packages/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(https://github.com/WordPress/gutenberg/pull/43403),
> 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.
> https://github.com/WordPress/gutenberg/pull/43403/files#diff-
> 796959a4b78efdd6a2af2ec364be6a4af2566a57041673f2fabbd153bc743cf9
New description:
In the WordPress core, there is a JavaScript-based
`WordCounter`(https://core.trac.wordpress.org/browser/trunk/src/js/_enqueues/wp/utils
/word-count.js) class to count the number of characters appropriately
based on language type. My understanding is that this logic is
incorporated into the
`@wordpress/wordcount`(https://github.com/WordPress/gutenberg/tree/trunk/packages/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(https://github.com/WordPress/gutenberg/pull/43403),
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.
https://github.com/WordPress/gutenberg/pull/43403/files#diff-
796959a4b78efdd6a2af2ec364be6a4af2566a57041673f2fabbd153bc743cf9
----
See also:
- #40759
--
--
Ticket URL: <https://core.trac.wordpress.org/ticket/57987#comment:29>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list