# Require a Title Before Publishing

> Prevents posts and pages from being published or scheduled without a title, avoiding "(no title)" entries in menus, feeds, search results and social shares.

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

## Code

```php
// Post types that need a title.
$wpide_title_required_types = array( 'post', 'page' );

foreach ( $wpide_title_required_types as $wpide_type ) {
    add_filter( "rest_pre_insert_{$wpide_type}", function ( $prepared, $request ) {
        $status = $request->get_param( 'status' ) ? $request->get_param( 'status' ) : get_post_status( (int) ( $prepared->ID ?? 0 ) );
        if ( ! in_array( $status, array( 'publish', 'future' ), true ) ) {
            return $prepared;
        }
        $title = null !== $request->get_param( 'title' ) ? $request->get_param( 'title' ) : get_post_field( 'post_title', (int) ( $prepared->ID ?? 0 ), 'raw' );
        $title = is_array( $title ) ? ( $title['raw'] ?? '' ) : (string) $title;

        return '' === trim( wp_strip_all_tags( $title ) )
            ? new WP_Error( 'wpide_title_required', 'Please add a title before publishing.', array( 'status' => 400 ) )
            : $prepared;
    }, 10, 2 );
}

// Classic editor and other code paths.
add_filter( 'wp_insert_post_data', function ( $data ) use ( $wpide_title_required_types ) {
    if ( in_array( $data['post_type'], $wpide_title_required_types, true ) && in_array( $data['post_status'], array( 'publish', 'future' ), true ) && '' === trim( wp_strip_all_tags( wp_unslash( $data['post_title'] ) ) ) && ! wp_is_serving_rest_request() ) {
        $data['post_status'] = 'draft';
    }
    return $data;
} );
```

## Notes

Drafts without a title can still be saved; add other post types to $wpide_title_required_types.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Require a Title 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/
