-
-
Save emersonthis/08126d6c425b8b3c5fed94a8aaa2c217 to your computer and use it in GitHub Desktop.
WordPress: Mandatory Excerpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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) { | |
$location = str_replace('&message=6', '', $location); | |
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