Skip to content

Instantly share code, notes, and snippets.

@swalkinshaw
Created May 14, 2012 18:25
Show Gist options
  • Save swalkinshaw/2695510 to your computer and use it in GitHub Desktop.
Save swalkinshaw/2695510 to your computer and use it in GitHub Desktop.
WordPress: Mandatory Excerpt
<?
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');
@reasadazim
Copy link

reasadazim commented Feb 6, 2018

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');

@kevinohashi
Copy link

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.

@lautiab
Copy link

lautiab commented Oct 11, 2018

I do really love you guys. I know closely none PHP and I needed this for a freelance project!!
I owe you'all one!

@marianssen
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment