-
-
Save swalkinshaw/2695510 to your computer and use it in GitHub Desktop.
<? | |
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'); |
Will this work with the latest versions of WP?
@vincepettit I just tried it on 4.4.2 and seem to work fine.
Looks like you can't PR gists... But I figured it out...
function excerpt_error_message_redirect($location) {
$location = str_replace('&message=6', '', $location);
return add_query_arg('excerpt_required', 1, $location);
}
this works for me and I have added some modification to this code for custom post type. Here is the code.
function mandatory_excerpt($data) {
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');
Has anyone had issues with this causing an over abundance of Auto-Drafts? After using this every time any of our users log into WordPress it creates an Auto-Draft for them. I tried moving it to a plugin but same thing. Disabling the plugin makes the problem disappear.
Edit: so it wasn't creating an unusual amount of Auto-Drafts, it was just doing what it always does, but the problem was that the mandatory_excerpt function was preventing drafts from being deleted. My only intuition to why that was happening is the line:
$data['post_status'] = 'draft'; // happens when deleting, too?
I ended up adding a few extra checks which helps (we have some additional post types and custom statuses).
function mandatory_excerpt($data) {
$excerpt = $data['post_excerpt'];
if ( ($data['post_type'] === 'post' || $data['post_type']==='poll') && empty($excerpt)) {
if ($data['post_status'] === 'publish') {
add_filter('redirect_post_location', 'excerpt_error_message_redirect', '99');
}
if ('deleted' !== $data['post_status'] && 'trash' !== $data['post_status']) {
$data['post_status'] = 'draft';
}
}
return $data;
}
Thank you! Just what I've been looking for and without the need for another plugin!
@ManyouRisms You would probably be better with the following logic:
function mandatory_excerpt($data) {
if (empty($data['post_excerpt'])) {
if ($data['post_status'] === 'publish') {
add_filter('redirect_post_location', 'excerpt_error_message_redirect', '99');
}
if ($data['post_status'] == 'publish' || $data['post_status'] == 'future' || $data['post_status'] == 'pending') {
$data['post_status'] = 'draft';
}
}
return $data;
}
Thank's for the logic. My office's iteration of WordPress doesn't allow for most plugins, so this kind of solution was necessary! Thanks again!
Thanks to the help from this Gist and comments, I made a class which does this with a few bells and whistles. It allows you to define different minimum lengths for different (custom) post types. Usage is simple:
$temp = new Mandatory_Excerpt();
$temp->add('post', 20); // Posts will now need to have 20 characters or more.
// add more minimum excerpt lengths here if required
Your code helped me a lot. I wanted to make excerpt field mandatory for my CPT but your code make excerpt field mendatory for PAGE and default POST also. However, I made slight change and that worked.
function mandatory_excerpt($data) {
//change your_post_type to post, page, or your custom post type slug
if ( 'your_post_type' == $data['post_type'] ) {
$excerpt = $data['post_excerpt'];
if (empty($excerpt)) { // If excerpt field is empty
// Check if the data is not drafed and trashed
if ( ( $data['post_status'] != 'draft' ) && ( $data['post_status'] != 'trash' ) ){
$data['post_status'] = 'draft';
add_filter('redirect_post_location', 'excerpt_error_message_redirect', '99');
}
}
}
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 field is empty! Excerpt is required to publish your recipe post.';
break;
default:
$message = 'Unexpected error';
}
echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}
add_action('admin_notices', 'excerpt_admin_notice');
If you want to only have this happen on a specific post type you can use some logic:
if (empty($data['post_excerpt']) && $data['post_type']=='post') {
changing 'post' to whatever post type you wish.
I do really love you guys. I know closely none PHP and I needed this for a freelance project!!
I owe you'all one!
The code works as expected on posts, but there is an issue with menu. Somehow, after updating a menu, the menu items get marked as pending. Removing your code fixed the issue.
@ccarpenterIT Yep, that's it.