Aller au contenu

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.

  • 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.
  1. Log in to WordPress with an administrator account.

  2. In the dashboard, go to Appearance → Theme Editor.

  3. Select your active theme and open the functions.php file.

  4. Add the following code snippet:

    // Limit maximum word count for posts
    function 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 here
    if ($word_count > $max_words) {
    // prevent publishing and show error
    wp_die('Error: Your post exceeds the maximum word count of ' . $max_words . ' words.');
    }
    }
    add_action('publish_post', 'ruachost_max_word_count');
    • Replace 1000 with the maximum number of words you want to allow.
    • When a post exceeds this limit, WordPress will block publishing and display an error message.
  5. Save the file.

  6. Test by creating a post → If it exceeds the word limit, publishing will be prevented.

  • 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é ?