making your posts password-protected by default

I originally wanted to be able to password protect a category. At the very least I wanted it to be password-protected, but preferably a username and password login. Since I have been unsuccessful for days at finding a solution I have now resorted to using WordPress’s built-in password protection for posts.

The issue I am having is I will be posting via e-mail and in order to have these posts password-protected I need to login to WordPress and then manually select password-protected and enter a password in the dashboard.

Read More

I would like to be able to have all posts that appear in a specific category be password-protected with the same password by default. Eliminating having to log in to WordPress and manually select password protect.

I know there is a function <?php post_password_required( $post ); ?> that I need to use but I am not sure how to implement it or where.

Related posts

Leave a Reply

2 comments

  1. Based on this WordPress StackExchange answer. Tested only with a regular dashboard. Publishing via email has to be tested, but I suppose the hook gets called in this kind of posting.

    add_action( 'save_post', 'wpse51363_save_post' );
    
    function wpse51363_save_post( $post_id ) {
    
        //Check it's not an auto save routine
         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
              return;
    
        //Check it's not an auto save routine
         if ( wp_is_post_revision( $post_id ) ) 
              return;
    
        //Perform permission checks! For example:
        if ( !current_user_can( 'edit_post', $post_id ) ) 
              return;
    
        $term_list = wp_get_post_terms(
            $post_id, 
            'category', 
            array( 'fields' => 'slugs' ) 
        );
    
        if( in_array ( 'the-category-slug', $term_list ) )
        {
            // Unhook this function so it doesn't loop infinitely
            remove_action( 'save_post', 'wpse51363_save_post' );
    
            // Call wp_update_post update, which calls save_post again. 
            wp_update_post( array( 
                'ID' => $post_id,
                'post_password' => 'default-password' ) 
            );
    
            // Re-hook this function
            add_action( 'save_post', 'wpse51363_save_post' );
        }
    }
    
  2.     add_filter( 'wp_insert_post_data', function( $data, $postarr ){
        if ( 'book' == $data['post_type'] && 'auto-draft' == $data['post_status'] ) {
            $data['post_password'] = wp_generate_password();
        }
        return $data;
    }, '99', 2 );