Once you publish a WordPress post (or Page), it is visible to the public (by default). In WordPress terms, this means a post with a Published status has a default visibility of Public, based on the settings in the Publish box of a Edit Post Screen.
Checking Password protected reveals a text field for entering a password, that will be required to view that post. The password is limited to 20 characters.
Checking Private makes your post visible only to your site’s Users with the role of Administrator or Editor.
Password Size Limitation
You can set the maxlength to a value of 20 when replacing the password protection form as WordPress will only save the first 20 characters due to database constraints.
1 2 3 4 5 6 7 8 9 10 | <?php function my_excerpt_protected( $excerpt ) { if ( post_password_required() ) $excerpt = '<em>[This is password-protected.]</em>'; return $excerpt; } add_filter( 'the_excerpt', 'my_excerpt_protected' ); ?> |
Add the Password Form to the Excerpt
With the get_the_password_form function, you can make the password form be the Excerpt for a password-protected post:
1 2 3 4 5 6 7 8 9 10 | <?php function my_excerpt_password_form( $excerpt ) { if ( post_password_required() ) $excerpt = get_the_password_form(); return $excerpt; } add_filter( 'the_excerpt', 'my_excerpt_password_form' ); ?> |
Hiding Password Protected Posts
Copy and paste the following code in your theme’s functions.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php // Filter to hide protected posts function exclude_protected($where) { global $wpdb; return $where .= " AND {$wpdb->posts}.post_password = '' "; } // Decide where to display them function exclude_protected_action($query) { if( !is_single() && !is_page() && !is_admin() ) { add_filter( 'posts_where', 'exclude_protected' ); } } // Action to queue the filter at the right time add_action('pre_get_posts', 'exclude_protected_action'); ?> |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: wordpress