Last active
November 2, 2022 12:08
-
-
Save elvismdev/60cb39da63dd97d87b8f2e64ddb1ba29 to your computer and use it in GitHub Desktop.
Require post title at backend WordPress editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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