# Require an Excerpt Before Publishing

> Blocks publishing posts that have no hand-written excerpt, so archive pages, search results and social shares always have a proper summary.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: editorial, excerpts, publishing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/content-require-excerpt/

## Code

```php
// Minimum excerpt length in characters.
$wpide_excerpt_min_chars = 50;
// Post types that need an excerpt.
$wpide_excerpt_types = array( 'post' );

add_filter( 'rest_pre_insert_post', function ( $prepared, $request ) use ( $wpide_excerpt_min_chars, $wpide_excerpt_types ) {
    $post_id = (int) ( $prepared->ID ?? 0 );
    $type    = $post_id ? get_post_type( $post_id ) : 'post';
    $status  = $request->get_param( 'status' ) ? $request->get_param( 'status' ) : get_post_status( $post_id );

    if ( ! in_array( $type, $wpide_excerpt_types, true ) || ! in_array( $status, array( 'publish', 'future' ), true ) ) {
        return $prepared;
    }

    $excerpt = null !== $request->get_param( 'excerpt' ) ? $request->get_param( 'excerpt' ) : get_post_field( 'post_excerpt', $post_id, 'raw' );
    $excerpt = is_array( $excerpt ) ? ( $excerpt['raw'] ?? '' ) : (string) $excerpt;

    if ( mb_strlen( trim( wp_strip_all_tags( $excerpt ) ) ) < $wpide_excerpt_min_chars ) {
        return new WP_Error( 'wpide_excerpt_required', sprintf( 'Please write an excerpt of at least %d characters before publishing.', $wpide_excerpt_min_chars ), array( 'status' => 400 ) );
    }

    return $prepared;
}, 10, 2 );
```

## Notes

Set $wpide_excerpt_min_chars to require a minimum length; drafts can still be saved.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Require an Excerpt Before Publishing" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
