Password protect any posts automatically upon publish

You can password protect all posts once they’re published using the following function.

add_action('publish_post', 'ppwp_pro_auto_protect_posts_when_publishing');
function ppwp_pro_auto_protect_posts_when_publishing( $post_id ) {
        if ( method_exists('PPW_Pro_Password_Services', 'protect_post_by_password' ) ) {
		$passwords_service = new PPW_Pro_Password_Services();
		$passwords_service->protect_post_by_password( $post_id );

To apply for pages or any custom post types, you should change the action accordingly.

add_action('publish_page', 'ppwp_pro_auto_protect_posts_when_publishing');
// Apply to My Books custom post type
add_action('publish_my_book', 'ppwp_pro_auto_protect_posts_when_publishing');