How can I hook into the post editor title field in order to change the HTML?

There is no hook to change the HTML of the input (only the enter_title_here filter to change the placeholder text). You could pull this off easily with jQuery, though. Try this in your functionality plugin or theme’s functions.php file:

// Add to the new post screen for any post type
add_action( 'admin_footer-post-new.php', 'wpse_add_required_attr_to_title_field' );

// Add to the post edit screen for any post type
add_action( 'admin_footer-post.php', 'wpse_add_required_attr_to_title_field' );

function wpse_add_required_attr_to_title_field() {
    ?>
    <script>
        jQuery(document).ready(function($){
            $('input[name=post_title]').prop('required',true);
        });
    </script>
    <?php
}

I should note, however, not knowing what your user base for this site’s administration looks like, that the prevention of submitting a form based solely on the required attribute isn’t implemented exactly the same across the board, so if this matters for your use case, you might want to look at an implementation that forces it’s own alert, like for example in the Force Post Title plugin.

Leave a Comment