Support » Plugins » Post minimum words count before publish for authors

  • Hello, I’m using a piece of code to check on minimum post words count for authors so that post are not published with very few words. If the criteria are not met, the post goes DRAFT.

    My plugin uses the “publish_post” action in which I do the check. A simplified version of the code that performs the check follows:

    static function publish_post_author_validation( $ID )
    {
    	$status = get_post_status( $ID );
    	if( ( 'publish' == $status ) && !current_user_can( 'manage_options' ) ) { // on publish, for all except admin
    		$options = get_option( 'myplug_options' );
    		$post = get_post( $ID, OBJECT );
    		$error = false;
    		// word count check
    		if( is_numeric( $options['posting_minimum_words_count'] ) ) {
    			$wctext = trim( $post->post_content );
    			$wctext = strip_tags( $wctext );
    			$wctext = str_replace( ' ', ' ', $wctext );
    			$word_count = str_word_count( $wctext );
    			if( $word_count < $options['posting_minimum_words_count'] ) {
    				$error = true;
    			}
    		}
    		if( $error ) {
    			// set post draft on error
    			$newpost = array(
    				'ID' => $ID,
    				'post_status' => 'draft',
    				'post_password' => ''
    			);
    			wp_update_post( $newpost );
    		}
    	}
    }

    It has worked fine so far, but lately there is an author who manages to override this check, I don’t know how. I’ve put the limit to 100 words, but he manages to publish the posts with 70 or 80 words even.

    I thought he could override it by using the quick edit in posts list screen, I’ve tested this possibility but the check is regularly performed in quick edit so there must be another way.

    Any idea how an author could override my filter on minimum post words count?

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter DrLightman

    (@drlightman)

    After some tests I discovered that if an author puts a future publish date, when WordPress does automatically publish the post, the function above doesn’t work. Now I have to understand why.

    Thread Starter DrLightman

    (@drlightman)

    Using the hook “transition_post_status” let me control other cases so I managed to fix the issue. Thank you for the invaluable help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Post minimum words count before publish for authors’ is closed to new replies.