Skip to content

Instantly share code, notes, and snippets.

@mrdulal
Created December 6, 2016 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdulal/6a8f67c9232a81e4b00fbc151fc93fd7 to your computer and use it in GitHub Desktop.
Save mrdulal/6a8f67c9232a81e4b00fbc151fc93fd7 to your computer and use it in GitHub Desktop.
Mandatory WordPress for custom post type and all post as well as page.
function mandatory_excerpt($data) {
//for multiple custom post type just add && 'custom-post-type-slug' != $data['post_type']
if ( 'custom-post-type-slug' != $data['post_type'] ) {
} else {
$excerpt = $data['post_excerpt'];
if (empty($excerpt)) {
if ($data['post_status'] === 'publish') {
add_filter('redirect_post_location', 'excerpt_error_message_redirect', '99');
}
$data['post_status'] = 'draft';
}
}
return $data;
}
add_filter('wp_insert_post_data', 'mandatory_excerpt');
function excerpt_error_message_redirect($location) {
remove_filter('redirect_post_location', __FILTER__, '99');
return add_query_arg('excerpt_required', 1, $location);
}
function excerpt_admin_notice() {
if (!isset($_GET['excerpt_required'])) return;
switch (absint($_GET['excerpt_required'])) {
case 1:
$message = 'Excerpt is required to publish a post.';
break;
default:
$message = 'Unexpected error';
}
echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}
add_action('admin_notices', 'excerpt_admin_notice');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment