ControlledChaos
10/31/2016 - 10:48 PM

Require WordPress post authors and editors to enter an excerpt before publishing a post. The post status of an existing post will be reverte

Require WordPress post authors and editors to enter an excerpt before publishing a post. The post status of an existing post will be reverted to that of Draft if updating with no excerpt entered.

<?php

function ccd_mandatory_excerpt( $data ) {
	$excerpt = $data['post_excerpt'];
	
	if ( empty( $excerpt ) && $data['post_status'] === 'publish' ) {
		add_filter( 'redirect_post_location', 'ccd_excerpt_error_message_redirect', 99 );
		$data['post_status'] = 'draft';
	}
	elseif ( empty( $excerpt ) && $data['post_status'] === 'trash' ) {
		$data['post_status'] = 'trash';
	}
	return $data;
}
add_filter( 'wp_insert_post_data', 'ccd_mandatory_excerpt' );

function ccd_excerpt_error_message_redirect( $location ) {
	$location = str_replace( '&message=6', '', $location );
	return add_query_arg( 'ccd_excerpt_required', 1, $location );
}

function ccd_excerpt_admin_notice() {
	if ( ! isset( $_GET['ccd_excerpt_required'] ) ) return;
	
	switch ( absint( $_GET['ccd_excerpt_required'] ) ) {
		case 1 :
		$message = 'Excerpt is required to publish.';
		break;
		
		default :
		$message = 'Unexpected error';
	}
	echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}
add_action( 'admin_notices', 'ccd_excerpt_admin_notice' );

?>

Mandatory Excerpts

WordPress Snippet