diff --git a/src/MasterSite.php b/src/MasterSite.php index 4a7dda0505..6af2f21d1c 100644 --- a/src/MasterSite.php +++ b/src/MasterSite.php @@ -134,6 +134,7 @@ protected function hooks(): void add_action('add_meta_boxes', [$this, 'add_meta_box_search'], 10, 2); add_action('save_post', [$this, 'save_meta_box_search'], 10, 2); add_action('save_post', [$this, 'set_featured_image'], 10, 2); + add_filter('wp_insert_post_data', [$this, 'require_post_title'], 10, 1); // Save "p4_global_project_tracking_id" on post save. add_action('save_post', [$this, 'save_global_project_id'], 10, 1); add_action('post_updated', [$this, 'clean_post_cache'], 10, 3); @@ -401,6 +402,24 @@ public function set_featured_image(int $post_id, WP_Post $post): void set_post_thumbnail($post_id, $matches[1][0]); } + /** + * Make post title mandatory on publish. + */ + public static function require_post_title(array $data): ?array + { + $types = Search\Filters\ContentTypes::get_all(); + if ( + empty($data['post_title']) + && !empty($data['post_status']) + && $data['post_status'] === 'publish' + && in_array($data['post_type'], array_keys($types)) + ) { + wp_die(__('Title is a required field.', 'planet4-master-theme-backend')); + } + + return $data; + } + /** * Generates hreflang metadata from countries.json template. */