Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@elvismdev
Last active November 2, 2022 12:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elvismdev/60cb39da63dd97d87b8f2e64ddb1ba29 to your computer and use it in GitHub Desktop.
Save elvismdev/60cb39da63dd97d87b8f2e64ddb1ba29 to your computer and use it in GitHub Desktop.
Require post title at backend WordPress editor
<?php
add_action( 'edit_form_advanced', 'force_post_title' );
function force_post_title( $post ) {
// List of post types that we want to require post titles for.
$post_types = array(
'post',
'report',
// 'event',
// 'project'
);
// If the current post is not one of the chosen post types, exit this function.
if ( ! in_array( $post->post_type, $post_types ) ) {
return;
}
?>
<script type='text/javascript'>
( function ( $ ) {
$( document ).ready( function () {
//Require post title when adding/editing Project Summaries
$( 'body' ).on( 'submit.edit-post', '#post', function () {
// If the title isn't set
if ( $( "#title" ).val().replace( / /g, '' ).length === 0 ) {
// Show the alert
if ( !$( "#title-required-msj" ).length ) {
$( "#titlewrap" )
.append( '<div id="title-required-msj"><em>Title is required.</em></div>' )
.css({
"padding": "5px",
"margin": "5px 0",
"background": "#ffebe8",
"border": "1px solid #c00"
});
}
// Hide the spinner
$( '#major-publishing-actions .spinner' ).hide();
// The buttons get "disabled" added to them on submit. Remove that class.
$( '#major-publishing-actions' ).find( ':button, :submit, a.submitdelete, #post-preview' ).removeClass( 'disabled' );
// Focus on the title field.
$( "#title" ).focus();
return false;
}
});
});
}( jQuery ) );
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment