Wordpress - Mandatory excerpt for custom post type
The code adds a filter to wp_insert_post_data
:
add_filter('wp_insert_post_data', 'mandatory_excerpt');
And here's the callback:
function mandatory_excerpt($data) {
$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;
}
The filter callback is passed $data
, which as per the Codex includes the following post data:
'post_author',
'post_date',
'post_date_gmt',
'post_content',
'post_content_filtered',
'post_title',
'post_excerpt',
'post_status',
'post_type',
'comment_status',
'ping_status',
'post_password',
'post_name',
'to_ping',
'pinged',
'post_modified',
'post_modified_gmt',
'post_parent',
'menu_order',
'guid'
Those data include 'post_type'
, which means you can use that inside the callback:
function mandatory_excerpt($data) {
if ( 'custom-posttype-slug' != $data['post_type'] ) {
return $data;
} 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;
}