Require Post Title for Custom Post Type(s)

needed a way to restrict publishing & saving draft posts (of any type) without a title. A quick internet search brought me to some decent responses on Stack Overflow, and one good one on Kellen Mace's site.

Previously at work, we had some issues in our CMS where some content editors would quickly create a post without a title, save the draft or publish, then add a title later. This caused a few issues in our headless service for select post types key to our business model. Without going into too much detail, I needed a way to restrict publishing & saving draft posts (of any type) without a title. A quick internet search brought me to some decent responses on Stack Overflow, and one good one on Kellen Mace‘s site.

Kellens article was from 2015, so I thought I would update it for current 2022 standard.

First we need to enqueue our javascript file by adding the code below to your themes functions.php file or use a functionality plugin (my preference).

Next, create a new javascript file. In this case I named it post-requirements.js:

If a title hasn’t been entered, we’ll show the user a popup box that says β€œA title is required.” and then putting the focus on the title field so they can enter one. This will work for the Save Draft & Publish buttons.
Austin
Austin

πŸ’πŸ½β€β™‚οΈ Husband to Jeana.
⚾️ Dodgers & Brewers.
πŸ’» PHP Engineer.
πŸŒπŸΌβ€β™‚οΈGolfer; ~13 HDC.
🌱 Hydroponic Gardner.
🍿 Plex nerd.
πŸš™ '15 WRX, '22 Model 3 LR, & '66 Corvette C2.

Follow me on Twitter @TheFrosty & Instagram @TheFrosty.

Articles: 293