• Hi Eliot,

    I have been using CCS on a daily basis and love it. Today I encountered a kind of bug though: When using [field excerpt] when the excerpt field is empty, the page seems to go into an endless loop (100% cpu).
    I guess this is because it falls back to the post content, but that’s where [field excerpt] is sitting. Is that expected behaviour and can I do something about it (apart from never using it when excerpt is empty)?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘[field excerpt] when excerpt is empty’ is closed to new replies.