8

I'm trying to create a plugin that alters the Add New Post page so the Visibility field says "Private" by default:

Status: Draft
Visibility: **Private**
Publish immediately

[Publish]

...as opposed to what WordPress normally assumes:

Status: Draft
Visibility: **Public**
Publish immediately

[Publish]

At the moment, I'm using the "wp_insert_post_data" filter, and that is allowing me to change any posts with a post_status of "auto-draft" to "private". While this works, there is an unintended side-effect: Changing the post_status to "private" seems to publish the post automatically, changing the button in the editor to "Update". Furthermore, if the user saves before specifying a title, the post will be published with the title "Auto-Draft".

Is there any way I can simply change Visibility to Private by the default, in a manner that doesn't auto-publish the post, and change the button to "Update"? In vanilla WordPress, users can manually change the visibility to Private, and the button remains as Publish... I just need to achieve that via a plugin. I also want to ensure that "public" can still be selected by the user, should they desire to.

Thanks!

3 Answers 3

-2

since you're developing a plug-in, I assume you don't want to touch any files outside of wp-content/plugins or ../themes for that matter.

However, if that's not the case, follow along:
Go to wp-admin/includes/meta-boxes.php and find:

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

Now change it to the obvious:

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

Again, this affects the meta-boxes.php file which is outside of the plugins folder. Nonetheless, I think the approach you should be taking is creating a new meta box, adding your custom visibility setting (i.e. private) and make the latter override the default WP visibility setting.

Best,
Chris

2
  • 2
    Solutions which require you to modify core files will always get steamrolled over when you do an update. Therefore they really aren't solutions. Commented Apr 18, 2011 at 19:21
  • Agreed, but unless I'm mistaken, I believe the part in bold is achievable via a regular plugin. If that's the case, then that's a decent solution, just slightly more work than what I wanted to do. Commented Apr 19, 2011 at 0:05
3

from the plugin; uses action 'post_submitbox_misc_actions' and some query to catch the user Publish form: http://wordpress.org/extend/plugins/private-post-by-default/

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' );

?>
1
  • This breaks drafts and auto-publishes posts when you do "Save Draft". Any other solutions?
    – user25977
    Commented Jan 13, 2013 at 23:38
2

The correct way to automatically mark a post as private is to do it with the wp_insert_post_data filter. It's very straightforward:

add_filter( 'wp_insert_post_data', 'mark_post_private' );
function mark_post_private( $data ) {
    if ( 'your_post_type_goes_here' == $data['post_type'] ) {
        $data['post_status'] = 'private';
    }

    return $data;
}
2
  • This is correct, but if you want the post to be able to be put into trash, you should check first if ( 'trash' != $post['post_status'] ).
    – alexg
    Commented Dec 13, 2019 at 11:19
  • Be VERY careful using this - I was using it fine for a while then I just had to remove it. It started publishing blank posts titled 'autodraft' by itself, which you then can't delete without switching themes first. I deleted this snippet, and it stopped happening. Commented Mar 17, 2023 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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