Forums

[resolved] How to set new posts visibility to private by default ? (14 posts)

  1. StoarceCreierul
    Member
    Posted 2 years ago #

    The scenario is registered users posting for other registered users only, using custom simple forms.

    Is there a plugin or way to hack the default visibility settings for new posts to private ?

  2. sagar_mehta
    Member
    Posted 2 years ago #

    I am looking for a solution to this myself.

    I'd love to learn how this can be done.

    Sagar

  3. sagar_mehta
    Member
    Posted 2 years ago #

    Just discovered how to do this:

    For this, you need to edit the file 'meta-boxes.php' located in wp-admin/includes/ folder.

    Open that file in NotePad and find the following code:

    $visibility = 'public';
    $visibility_trans = __('Public');

    Now, all you need to do is change it do the following:

    $visibility = 'private';
    $visibility_trans = __('Private');

    Done.

    You can even hide the Visibility option so that users can't change it. To do that, find the following code in the same file:

    <a href="#visibility" class="edit-visibility hide-if-no-js"><?php _e('Edit'); ?></a>
    
    <div id="post-visibility-select" class="hide-if-js">
    
    <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-public" value="public" <?php checked( $visibility, 'public' ); ?> /> <label for="visibility-radio-public" class="selectit"><?php _e('Public'); ?></label><br />
    <?php if ($post_type == 'post'): ?>
    <span id="sticky-span"><input id="sticky" name="sticky" type="checkbox" value="sticky" <?php checked(is_sticky($post->ID)); ?> tabindex="4" /> <label for="sticky" class="selectit"><?php _e('Stick this post to the front page') ?></label><br /></span>
    <?php endif; ?>
    <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"><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>
    <input type="radio" name="visibility" id="visibility-radio-private" value="private" <?php checked( $visibility, 'private' ); ?> /> <label for="visibility-radio-private" class="selectit"><?php _e('Private'); ?></label><br />
    
    <p>
     <a href="#visibility" class="save-post-visibility hide-if-no-js button"><?php _e('OK'); ?></a>
     <a href="#visibility" class="cancel-post-visibility hide-if-no-js"><?php _e('Cancel'); ?></a>
    </p>
    </div>

    Replace that with the following:

    <!-- <a href="#visibility" class="edit-visibility hide-if-no-js"><?php _e('Edit'); ?></a>
    
    <div id="post-visibility-select" class="hide-if-js">
    
    <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-public" value="public" <?php checked( $visibility, 'public' ); ?> /> <label for="visibility-radio-public" class="selectit"><?php _e('Public'); ?></label><br />
    <?php if ($post_type == 'post'): ?>
    <span id="sticky-span"><input id="sticky" name="sticky" type="checkbox" value="sticky" <?php checked(is_sticky($post->ID)); ?> tabindex="4" /> <label for="sticky" class="selectit"><?php _e('Stick this post to the front page') ?></label><br /></span>
    <?php endif; ?>
    <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"><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>
    <input type="radio" name="visibility" id="visibility-radio-private" value="private" <?php checked( $visibility, 'private' ); ?> /> <label for="visibility-radio-private" class="selectit"><?php _e('Private'); ?></label><br />
    
    <p>
     <a href="#visibility" class="save-post-visibility hide-if-no-js button"><?php _e('OK'); ?></a>
     <a href="#visibility" class="cancel-post-visibility hide-if-no-js"><?php _e('Cancel'); ?></a>
    </p>
    </div> -->

    Basically, just commenting out the part by using <!-- and -->. A plugin for this would be awesome.

    WARNING: Commenting out the form will mean that NO ONE can make a public or password protected post. And all your pages will be Private by default without an option to change them back to public.

    The work around is that after you create a page or write a post you want to be public, just go to the 'Edit Posts' or 'Edit Pages' screen and uncheck the 'Private Post' / 'Private Page' checkbox under Quick Edit.

    Regards,
    Sagar

  4. StoarceCreierul
    Member
    Posted 2 years ago #

    This is it, thank you Sagar !

    Now how do we get rid of the "Private:" text at the beginning of the title ?

  5. sagar_mehta
    Member
    Posted 2 years ago #

    To hide the Visiblity: Private part that shows up, find the following code in the same file:

    <?php _e('Visibility:'); ?> <span id="post-visibility-display"><?php
    
    if ( 'private' == $post->post_status ) {
    	$post->post_password = '';
    	$visibility = 'private';
    	$visibility_trans = __('Private');
    } elseif ( !empty( $post->post_password ) ) {
    	$visibility = 'password';
    	$visibility_trans = __('Password protected');
    } elseif ( $post_type == 'post' && is_sticky( $post->ID ) ) {
    	$visibility = 'public';
    	$visibility_trans = __('Public, Sticky');
    } else {
    	$visibility = 'Private';
    	$visibility_trans = __('Private');
    }
    
    echo esc_html( $visibility_trans ); ?>

    And replace it with the following:

    <!-- <?php _e('Visibility:'); ?> <span id="post-visibility-display"><?php
    
    if ( 'private' == $post->post_status ) {
    	$post->post_password = '';
    	$visibility = 'private';
    	$visibility_trans = __('Private');
    } elseif ( !empty( $post->post_password ) ) {
    	$visibility = 'password';
    	$visibility_trans = __('Password protected');
    } elseif ( $post_type == 'post' && is_sticky( $post->ID ) ) {
    	$visibility = 'public';
    	$visibility_trans = __('Public, Sticky');
    } else {
    	$visibility = 'Private';
    	$visibility_trans = __('Private');
    }
    
    echo esc_html( $visibility_trans ); ?> -->

    Again, simply commenting out the section :)

    Hope this helps :)

    Regards,
    Sagar

  6. StoarceCreierul
    Member
    Posted 2 years ago #

    I commented out the section but the private post still shows up with that text at the beginning:
    Private: Hello World!

    We should change something in /wp-includes/post-template.php on line 116.
    Here's the complete Retrieve post title function:

    function get_the_title( $id = 0 ) {
    	$post = &get_post($id);
    
    	$title = $post->post_title;
    
    	if ( !is_admin() ) {
    		if ( !empty($post->post_password) ) {
    			$protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
    			$title = sprintf($protected_title_format, $title);
    		} else if ( isset($post->post_status) && 'private' == $post->post_status ) {
    			$private_title_format = apply_filters('private_title_format', __('Private: %s'));
    			$title = sprintf($private_title_format, $title);
    		}
    	}
    	return apply_filters( 'the_title', $title, $post->ID );
    }
  7. sagar_mehta
    Member
    Posted 2 years ago #

    Oh ok, I thought you were talking about the 'Visibility: Private' section that shows up on the right hand side on the 'Write Post' page.

    To remove the "Private: " part from the title of the post, change the following line of code in your code above:
    $private_title_format = apply_filters('private_title_format', __('Private: %s'));

    To this:
    $private_title_format = apply_filters('private_title_format', __('%s'));

    That should do it :)

    Sagar

  8. StoarceCreierul
    Member
    Posted 2 years ago #

    Thank you !

  9. mattloak
    Member
    Posted 1 year ago #

    Is there any way to make the post default to Private without editing core files? Can this be done with filters in the functions.php file?

  10. gewone
    Member
    Posted 1 year ago #

    Big thanks for this. Perfect solution for your private life journal. Hopefully there will be a designer setting in the actual GUI somewhere soon (WP v3.1 perhaps?). I love the blogging engine anyways. ♥

  11. fireghost
    Member
    Posted 1 year ago #

    after set it as private, i can't search it's content even after I log in, what can i do? thanks!!

  12. jorve
    Member
    Posted 1 year ago #

    my problem with this workaround is that, once the post is made public, the page still defaults to private setting, so that if the page is updated, it becomes "privately published" once again.

    Trying to figure out a way to change this....

  13. jorve
    Member
    Posted 1 year ago #

    nvm, I figured it out..... if (in the meta-boxes.php file) you replace:

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

    with:

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

    then your posts will default to Private, but once they have been published publicly, they will remain public.

  14. pat.heard
    Member
    Posted 1 year ago #

    If you want to do it without editing the core files, you can hook into the post_submitbox_misc_actions action and add some jQuery that will fire onload:

    function default_post_visibility(){
    	global $post;
    
    	if ( 'publish' == $post->post_status ) {
    		$visibility = 'public';
    		$visibility_trans = __('Public');
    	} elseif ( !empty( $post->post_password ) ) {
    		$visibility = 'password';
    		$visibility_trans = __('Password protected');
    	} elseif ( $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>
    	<?php
    }
    add_action( 'post_submitbox_misc_actions' , 'default_post_visibility' );

Topic Closed

This topic has been closed to new replies.

About this Topic