Paulund
2015-12-08 #wordpress

Set WordPress Posts To Private By Default

The following code snippet will allow you to set all your posts to private as default. We do this by using the action post_submitbox_misc_actions and inserting jQuery into the page to change the selected visibility setting. First this will check to see what the current post status is. If the post status is publish then we can set the visibility status to publish. Then we check to see if the post password is not empty, if the password is set we then set the visibility to password. We also have to check to see if the post is sticky then we change the words in the visibility options. If the current post status doesn't match any of these then we will set the post visibility to private, making this the default.


add_action( 'post_submitbox_misc_actions' , 'default_posts_to_private' );
function default_posts_to_private()
{
    global $post;

    if ( $post->post_status == 'publish' ) {
        $visibility = 'public';
        $visibility_trans = __('Public');
    } elseif ( !empty( $post->post_password ) ) {
        $visibility = 'password';
        $visibility_trans = __('Password protected');
    } elseif ( $post->post_type == 'post' && is_sticky( $post->ID ) ) {
        $visibility = 'public';
        $visibility_trans = __('Public, Sticky');
    } else {
        $post->post_password = '';
        $visibility = 'private';
        $visibility_trans = __('Private');
    } 
?>

    <script type="text/javascript">
        (function($){
            try {
                $('#post-visibility-display').text('<?php echo $visibility_trans; ?>');
                $('#hidden-post-visibility').val('<?php echo $visibility; ?>');
                $('#visibility-radio-<?php echo $visibility; ?>').attr('checked', true);
            } catch(err){}
        }) (jQuery);
    </script>
    &lt;?php
}

This plugin is available on the WordPress.org plugin directory Private Posts By Default but there is an error in the code with checking the post type is post so if you need to do this functionality then use the above code snippet.