Hi,
If I was you, I made the excerpt field required, or use a (new) custom field to add a separate text block to your content.
To make the excerpt required, you can add this code to your functions.php (of your child theme):
add_filter('wp_insert_post_data', 'mandatory_excerpt');
function mandatory_excerpt($data) {
/** Run excerpts only on certain post types */
$screen = get_current_screen();
if ((isset($data['post_type'])) && (isset($_REQUEST['action']))) {
$post_type=$data['post_type'];
$post_type_slug_excerpt_required= 'post';
$post_action=$_REQUEST['action'];
if (($post_type_slug_excerpt_required == $post_type) && ('editpost' == $post_action)) {
//This filter is processing the correct post type proceed
$excerpt = $data['post_excerpt'];
if (empty($excerpt)) {
//No excerpt!
if ($data['post_status'] === 'publish') {
add_filter('redirect_post_location', 'excerpt_error_message_redirect', '99');
}
//Change to draft
$data['post_status'] = 'draft';
}
}
}
return $data;
}
function excerpt_error_message_redirect($location) {
remove_filter('redirect_post_location', 'excerpt_error_message_redirect', '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');
Source
Peter
Thread Starter
0rca
(@0rca)
Hi Peter,
thank you very much! I will have a look at the code and then see if it is feasible for our customer to make the excerpt mandatory.
Thanks again
Michael