Require title for pages

Is there a way to force users to write a page title before publishing? I have found example of requiring post titles, but not page titles.

Related posts

Leave a Reply

2 comments

  1. Start with downloading the plugin called Force Post Title.

    Here’s the plugin with one row (2 with the comment line) added to the bottom, based on our comments.

    What happens is that a small jQuery script is added to the page Create Post/Page. The script will check if the title field is empty when the user clicks the submit button.

    Since this is such a small plugin, you could easily modify it and copy it to your functions.php. That way, you won’t have to edit an existing plugin which will give you a headache once you update it later on.

    I should also mention that I found something (a filter) that could work in wp-includes/post.php, row 2489. I did some quick testing without any results though.

    /*
    Plugin Name: Force Post Title
    Plugin URI: http://appinstore.com
    Description: Forces user to assign a Title to a post before publishing 
    Author: Jatinder Pal Singh
    Version: 0.1
    Author URI: http://appinstore.com/
    */ 
    function force_post_title_init() 
    {
      wp_enqueue_script('jquery');
    }
    function force_post_title() 
    {
      echo "<script type='text/javascript'>n";
      echo "
      jQuery('#publish').click(function(){
            var testervar = jQuery('[id^="titlediv"]')
            .find('#title');
            if (testervar.val().length < 1)
            {
                jQuery('[id^="titlediv"]').css('background', '#F96');
                setTimeout("jQuery('#ajax-loading').css('visibility', 'hidden');", 100);
                alert('POST TITLE is required');
                setTimeout("jQuery('#publish').removeClass('button-primary-disabled');", 100);
                return false;
            }
        });
      ";
       echo "</script>n";
    }
    add_action('admin_init', 'force_post_title_init');
    add_action('edit_form_advanced', 'force_post_title');
    // Add this row below to get the same functionality for page creations.
    add_action('edit_page_form', 'force_post_title');
    
  2. I just modified @hampusn answer a bit to make the integration better. Instead of using an alert() it puts a nicely formatted box under the title.

    It wraps the jQuery code with the standard .ready() function.

    I also only wanted to do it for a certain custom post type so I left the snippet in but you can just remove the $post_type check if future readers don’t need this.

    Finally I wrapped the validation message in _(). You could theoretically customise this per post type if you wanted to reassign it within the main if statement.

    function rtp_force_post_title_init() 
    {
      wp_enqueue_script('jquery');
    }
    function rtp_force_post_title( $post ) 
    {
      $post_type = get_post_type();
      $validation_message = _("The title field must be filled out.");
    
      if('exhibitor' === $post_type) {
        echo "<script type='text/javascript'>n";
        echo "
        jQuery( document ).ready(function() {
          jQuery('#publish').click(function(){
            var testervar = jQuery('[id^="titlediv"]').find('#title');
            if (testervar.val().length < 1)
            {
              setTimeout("jQuery('#ajax-loading').css('visibility', 'hidden');", 100);
              var validator_snippet = '<div style="padding: 10px; color: #fff; margin-top: -3px; background: #F55E4F;">" . $validation_message . "</div>';                    
              jQuery('[id^="titlediv"]').find('#titlewrap').append(validator_snippet);          
              setTimeout("jQuery('#publish').removeClass('button-primary-disabled');", 100);
              return false;
            }
          });
        });n";
        echo "</script>n";
      }
    }
    add_action( 'admin_init', 'rtp_force_post_title_init' );
    add_action( 'edit_form_after_title', 'rtp_force_post_title' );