This is proving to be a challenge.
I’m trying to make the excerpt a required field, but only when editing/saving a post in a custom post type.
The following code makes the excerpt a required field for all posts, but doesn’t take into account for narrowing its affect to a single custom post type.
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;
}
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');
The code adds a filter to
wp_insert_post_data
:And here’s the callback:
The filter callback is passed
$data
, which as per the Codex includes the following post data:Those data include
'post_type'
, which means you can use that inside the callback:The solution to the problem with posts not being able to be removed or even published is adding an extra check to make sure the
mandatory_excerpt()
function only fires when there is no$_GET['action']
provided. Otherwise, the function will always return an error when removing a post or changing it’s publish status.So the altered function would be:
I don’t have enough reputation on this site to comment.
Note that the code you’re using doesn’t have the appropriate checks for the post status. As a result, your admin dashboard will fill up with lots of Auto Drafts which are usually empty, which will never be cleaned up.
A simple fix is to do something like:
It seems the above solution did not work so well for me (as @slowaways mentioned earlier, with WordPress 5+).
So I came up with a new solution. Since I am using ACF, I created a new ‘short description’ field (‘extrait’) and made it mandatory. So I can use the validation features of ACF, and update excerpt value on the fly :
To give credit where credit is due i found this code here : https://gist.github.com/MWDelaney/fd0bed89b891e3ff0850428703786397
Hope this helps.