We earlier discussed the manual way by which one can use password protection for WordPress posts, but one can also do the same by using the snippets, the short codes. The following article we will discuss all those snippets which can help you in keeping your posts or pages password protected.
Protecting Custom Fields
Unless and until the correct password is entered, WordPress does not show or display a password-protected post’s Content or Excerpt. But, you do also notice that the post’s Custom Field(s) data is not protected and can still be shown.
In order to make it stopped from displaying, you need to wrap your single.php or page.php files get_post_meta calls with adding a conditional statement using: post_password_required. You can also use the post_password_required function within in an if statement to do other customization.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php if ( ! post_password_required() ) { // Code to fetch and print CFs, such as: $key_1_value_1 = get_post_meta( $post->ID, 'key_1', true ); echo $key_1_value_1; } ?> |
Customized The Protected Text
Using short codes will also give you freedom to change the default text in the password form. You can change the default text for the password-protected post Excerpt by inserting the password form into the post Excerpt by using the following code in your WordPress functions.php file.
The password form basically displays the default message: “This post is password protected. To view it please enter your password below:” Using the snippet will return the form with different text and different html structure. Here do make sure that you don’t use print or echo function at all while using the snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php function my_password_form() { global $post; $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID ); $o = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post"> ' . __( "To view this protected post, enter the password below:" ) . ' <label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" /> </form> '; return $o; } add_filter( 'the_password_form', 'my_password_form' ); ?> |
Protected Excerpt Text
Using the following short code will change the password excerpt text. The default Excerpt of a password-protected post is: “There is no excerpt because this is a protected post.” While after using the code will replace that text with the one you wanted. You are also allowed to use HTML here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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
You can also print out the password form instead of the Excerpt message. Using the following snippet will let you make the password form to be the Excerpt for a password protected post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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
You do often want to have your password protected posts to show up on different places in your site. You can use the following snippet in your theme’s functions.php file to hide the password protected posts from these pages without affecting your set pagination structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?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'); ?> |
Photo Credit: Flickr/Carlos Varela