3

I don't know how to make a plugin so I can't do what's suggested here How can I make it so the Add New Post page has Visibility set to Private by default?

so what's alternative ?

3

Accepted solution is not the correct answer to change the visibility of any post type to any status. Below code is the right way to change the post status.

function set_post_type_status_private( $status, $post_id ) {
    $status = 'private';
    return $status;
}
add_filter( 'status_edit_pre', 'set_post_type_status_private', 10, 2 );

Updated:

The above filter will change the post status to Private when user will hit Save Draft or Publish button. So on edit page load if you see Status Public then don't worry.

There is one more filter available to change the status before saving into database. The filter is status_save_pre but I didn't find any documentation on this page so I wrote below code to test it.

function save_post_type_status_private( $status ) {
    $status = 'private';
    return $status;
}
add_filter( 'status_save_pre', 'save_post_type_status_private', 10, 1 );

The above filter saves the post as Private post type as soon as edit page load so one may want to use this filter over status_edit_pre but if I use status_save_pre filter I run into an issue, I can't delete any post. So I prefer 'status_edit_pre' over 'status_save_pre' until WordPress team fix this bug.

| improve this answer | |
6

Found this on WordPress forums:

You can just add this to functions.php. I've tested once and seemed to work fine.

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' );
| improve this answer | |
  • Do not ever edit WP core files as an update will undo your changes. A plugin or themes function.php file is appropriate. Just adding this for newer WP developers. – Ryan Bayne Jul 27 '19 at 1:57
1

The alternative would be to find someone else to make a plugin.

If you're using WordPress.com (meaning you can't install arbitrary plugins on your site), then you're out of luck. But for a self-hosted WordPress installation, you'll need to write a plugin and install it on your site. There are no alternatives when it comes to adding custom functionality.

| improve this answer | |
0

The accepted answer doesn't work for me. Here's a simple solution. It will only run the first time you publish any post.

function tse_make_post_private( $new_status, $old_status, $post ) {
  // check if is published and the post type is "post"
  if ( 'publish' === $new_status && $post->post_type === 'post' ) {
    // check if the post status is not private
    if ( $post->post_status != 'private' ) {
      // change the post status to private
      $post->post_status = 'private';
      // update the post
      wp_update_post( $post );
    }
  }
}
add_action('transition_post_status', 'tse_make_post_private', 10, 3);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.