Make post password required to publish

I am making a site which will only have password protected pages. I would like it so that the password box is open by default, and is a required fields to publish the post. Ideally I would also remove the ‘public’ and ‘private’ options.

Is this possible?

Related posts

Leave a Reply

3 comments

  1. You would be better suited by allowing members to sign up on your site, then giving each uses level permissions to read different posts. A plugin like Members might work for this.

    But, if you really want to do what you asked, you’re going to have to do some javascript hacking or completely remove the publish meta box and roll your own. There’s not a ton of filters in there for use, unfortunately.

    To do the JS way, you’d need to enqueue your script…

    <?php
    add_action( 'admin_print_scripts-post.php', 'wpse38806_enqueue_js' );
    add_action( 'admin_print_scripts-post-new.php', 'wpse38806_enqueue_js' );
    function wpse38806_enqueue_js()
    {
        if( 'post' != get_post_type() ) return;
    
        wp_enqueue_script(
            'wpse38806js',
            plugin_dir_url( __FILE__ ) . 'private.js',
            array( 'jquery' ),
            NULL,
            true
        );
    }
    

    … Which would probably look something like this:

    jQuery(document).ready(function(){
        // hide the stuff you don't want
        jQuery('label[for=visibility-radio-public], label[for=visibility-radio-private]').hide();
        jQuery('input#visibility-radio-public, input#visibility-radio-private').hide();
    
        // trigger clicks to open the various things
        jQuery('a.edit-visibility').trigger('click');
        jQuery('input#visibility-radio-password').trigger('click');
    
        // stop form submission if there's no password, add an error message
        jQuery('form#post').submit(function(){
            if( ! jQuery('input#post_password').val() ){
                jQuery('img#ajax-loading').hide();
                jQuery('input#publish').removeClass('button-primary-disabled');
                jQuery('div.wrap h2').after('<div id="message" class="error"><p>Please enter a password!</p></div>');
                return false;
            }
        });
    });
    

    To roll your own submit meta box, you can add an action to add_meta_boxes_post and in the hooked function, remove the current submit box, and add your own.

    <?php
    add_action('add_meta_boxes_post', 'wpse38806_hijack_meta_boxes' );
    function wpse38806_hijack_meta_boxes( $post )
    {
        remove_meta_box( 'submitdiv', $post->post_type, 'side' );
        add_meta_box('submitdiv-2', __('Publish'), 'wpse38806_post_submit_meta_box', $post->post_type, 'side', 'high');
    }
    

    In your wpse38806_post_submit_meta_box callback, you should copy the contents of post_submit_meta_box, found int wp-admin/includes/meta-boxes.php, and take out the relevant sections. It’s a very long function, so I’m not going to post the entire thing. I just removed the few bits we don’t want, and added a few style attribute to force things to be displayed.

    function wpse38806_post_submit_meta_box($post) {
        // snip snip
        ?>
    
    <div class="misc-pub-section " id="visibility">
    <?php if ( $can_publish ) { ?>
    
    <div id="post-visibility-select" class="hide-if-js" style="display:block">
    <input type="hidden" name="hidden_post_password" id="hidden-post-password" value="<?php echo esc_attr($post->post_password); ?>" />
    <?php if ($post_type == 'post'): ?>
    <input type="checkbox" style="display:none" name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" <?php checked(is_sticky($post->ID)); ?> />
    <?php endif; ?>
    <input type="hidden" name="hidden_post_visibility" id="hidden-post-visibility" value="<?php echo esc_attr( $visibility ); ?>" />
    
    <input type="radio" name="visibility" id="visibility-radio-password" value="password" <?php checked( $visibility, 'password' ); ?> /> <label for="visibility-radio-password" class="selectit"><?php _e('Password protected'); ?></label><br />
    <span id="password-span" style="display:block"><label for="post_password"><?php _e('Password:'); ?></label> <input type="text" name="post_password" id="post_password" value="<?php echo esc_attr($post->post_password); ?>" /><br /></span>
    </div>
    <?php } ?>
    
    </div>
    <?php
    // snip snip
    }
    

    Here’s all that as a plugin.

  2. You could do this using Javascript:

    add_action('admin_head-post.php', 'require_post_password');
    add_action('admin_head-post-new.php', 'require_post_password');
    
    function require_post_password(){
        echo "
            <script type='text/javascript'>
                    jQuery(document).ready(function($) {
                        $('#publish').bind('click', function(){
                                if($('#post_password').val() == ''){
                                        $('#ajax-loading').hide();
                                        alert('Please enter a post password');
                                        return false;
                                }
                        });
                        $('#visibility-radio-private, label[for=visibility-radio-private], #visibility-radio-public, label[for=visibility-radio-public]').remove();
                        $('#hidden-post-visibility').val('password');
                        $('#post-visibility-display').text('Password Protected');
                    });
            </script>
        ";
    }