Look in the comments to Vikas’ solution… much cleaner solution!
The number of functions built into WordPress is kind of incredible. Some of them get lost in the shuffle, one of those actions is post_submitbox_misc_actions
.
This action gives you the ability to add or adjust content or fields for a post or page when the submit/update box is clicked.
At this point you are probably asking ‘Why would I want to do this?’.
One specific instance I used it for recently was for a client who needed their content to be set to private. Using post_submitbox_misc_actions, WordPress’ built in visibility settings, and some JavaScript, I could change the default visibility setting from public to private.
add_action( 'post_submitbox_misc_actions' , 'MyHotNewAction' );
function MyHotNewAction(){
global $post;
if ($post->post_type != 'MyCustomPostType')
return;
$post->post_password = '';
$visibility = 'private';
$visibility_trans = __('Private');
?>
<script type="text/javascript">
(function($){
try {
$('#post-visibility-display').text('');
$('#hidden-post-visibility').val('');
$('#visibility-radio-').attr('checked', true);
} catch(err){}
}) (jQuery);
</script>
This doesn’t set the default value. It forces the formular to “private” when clicking the button. That’s a dirty hack and not very elegant especially regarding usability.
Thanks for the feedback! I do not necessarily disagree that it is a “hack”, but it was merely a suggestion as to something I did in one application.
What alternative solution do you propose that would be more clean and elegant?
The correct solution is
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 );
I hope this is will help other developers.
Thanks for the information Vikas!