Setting the maximum word count for WordPress posts
Ce contenu n’est pas encore disponible dans votre langue.
By default, WordPress does not limit the length of posts. However, you may want to enforce a maximum word count to maintain consistency, improve readability, or meet editorial guidelines. On ruachost.com, you can achieve this by adding a custom PHP function.
Why Limit Word Count?
Section titled “Why Limit Word Count?”- Ensures posts remain concise and focused.
- Helps maintain a consistent style across your site.
- Prevents contributors from publishing overly long content.
- Improves user experience and readability.
Steps to Set Maximum Word Count
Section titled “Steps to Set Maximum Word Count”-
Log in to WordPress with an administrator account.
-
In the dashboard, go to Appearance → Theme Editor.
-
Select your active theme and open the functions.php file.
-
Add the following code snippet:
// Limit maximum word count for postsfunction ruachost_max_word_count($post_id) {$post = get_post($post_id);$content = strip_tags($post->post_content);$word_count = str_word_count($content);$max_words = 1000; // set your maximum word count hereif ($word_count > $max_words) {// prevent publishing and show errorwp_die('Error: Your post exceeds the maximum word count of ' . $max_words . ' words.');}}add_action('publish_post', 'ruachost_max_word_count');- Replace
1000with the maximum number of words you want to allow. - When a post exceeds this limit, WordPress will block publishing and display an error message.
- Replace
-
Save the file.
-
Test by creating a post → If it exceeds the word limit, publishing will be prevented.
Important Notes
Section titled “Important Notes”- Always back up your theme before editing
functions.php. - Use a child theme to preserve changes during updates.
- You can customize the error message to guide authors (e.g., “Please shorten your post to under 1000 words”).
Cette page vous a-t-elle aidé ?
Merci pour votre retour.