How to password protect WordPress custom page template & ACF custom fields

By default, WordPress in general and our PPWP plugin in particular only protect a post’s content and excerpt. Their custom field data is not protected. In other words, these custom field’s content will still display even if the post is protected.

So if your websites are built based on custom templates that don’t use the standard the_content() and custom fields created via WordPress post meta or plugins like ACF, you have to wrap your content or these calls (in your custom WordPress template) with a conditional check using the default WordPress post_password_required.

How to password protect WordPress custom page template

  1. Open the template file under your theme folder
  2. Wrap your custom fields or content with the following code snippet
<?php
/**
 * Template Name: Password-Protected Custom Template
 */
?>

<?php get_header(); ?>

<div class="entry-content">
<?php
// STARTS - wrapp your content with this conditional statement
if ( post_password_required() ) :

    // if your post is password protected with our Pro version, show our password form instead
    echo get_the_password_form();

/* display the password protected content if the correct password is entered */ 
else :

    // display any custom private content
    echo 'custom HTML content';
    // fetch & print WordPress custom field value via get_post_meta calls
    $custom_field_content = get_post_meta( $post->ID, 'key_1', true );
    echo $custom_field_content;
    
    // fetch & print ACF fields
    the_field( 'acf_introduction_text' );

endif;
// ENDS - hide custom fields with PPWP password protection
?>
</div>

<?php get_footer();?>
Lasted updated on June 3, 2020