Created
November 6, 2022 15:52
-
-
Save AndreFCAmorim/d796e395d19d75a7bcb286bf49e8ed09 to your computer and use it in GitHub Desktop.
Set a Minimum Word Count for Posts on WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Prevent publishing posts under a minimum number of words. | |
* | |
* @param int $post_id The id of the post. | |
* @param WP_Post $post The post object. | |
* | |
* @return void | |
*/ | |
function wpcode_snippet_publish_min_words( $post_id, $post ) { | |
// Edit the line below to set the desired minimum number of words. | |
$word_count = 100; | |
if ( str_word_count( $post->post_content ) < $word_count ) { | |
wp_die( | |
sprintf( | |
// Translators: placeholder adds the minimum number of words. | |
esc_html__( 'The post content is below the minimum word count. Your post needs to have at least %d words to be published.' ), | |
absint( $word_count ) | |
) | |
); | |
} | |
} | |
add_action( 'publish_post', 'wpcode_snippet_publish_min_words', 9, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment