Enhance User Experience with a Stylish and Responsive Password Form
Content Protection, Tools, Websites, WordPress customization

How to Create a Custom Password-Protected Page in WordPress: A Complete Guide

Enhance User Experience with a Stylish and Responsive Password Form

Learn how to transform WordPress’s default password-protected pages into professionally styled, responsive forms with this step-by-step guide. Perfect for membership sites, exclusive content, or sites under development.

Content:

Password-protected pages are essential for many WordPress websites, whether you’re creating member-only content, developing a site, or restricting sensitive information. However, the default WordPress password form lacks visual appeal and customization options.
In this comprehensive guide, we’ll show you how to create a beautiful, fully customized password-protected page using a simple code snippet, without installing additional plugins.

The Problem with Default WordPress Password Protection

The standard WordPress password form has several limitations:

  1. Basic styling that doesn’t match your site’s design
  2. Limited layout options with the password label and field side by side
  3. No easy way to customize the text or structure
  4. Poor alignment and positioning on the page

The Solution: A Custom Password Protection Snippet

Our solution uses a single code snippet that includes both PHP to restructure the form and CSS to style it. This approach is lightweight, efficient, and works with any WordPress theme.

  • Step 1: Install the [ Code Snippets Plugin ]
    First, install and activate the “Code Snippets” plugin:
    • Go to Plugins > Add New in your WordPress dashboard
    • Search for “Code Snippets”
    • Install and activate the plugin
  • Step 2: Create Your Custom Password Form Snippet
    • Navigate to Snippets > Add New
    • Name your snippet “Custom Password Protected Page”
    • Paste the following code:
				
					// Customize the password-protected form
function custom_password_protected_form() {
    global $post;
    $label = 'pwbox-' . ( empty( $post->ID ) ? rand() : $post->ID );
    $output = '<div class="password-container">
        <h3>Protected Content</h3>
        <p>This content is password protected. To view it, please enter your password below:</p>
        <form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
            <input name="post_password" id="' . $label . '" type="password" size="20" placeholder="Enter password" />
            <input type="submit" name="Submit" value="Access" />
        </form>
    </div>';
    
    return $output;
}
add_filter( 'the_password_form', 'custom_password_protected_form' );

// Add CSS styles only when needed
function custom_password_form_css() {
    // Load styles only if the page is password protected
    if (post_password_required()) {
        echo '<style>
        .password-container{display:flex;flex-direction:column;justify-content:center;align-items:center;width:100%;height:400px;margin:0 auto;text-align:center;padding:20px;background-color:#f7f7f7}
        .password-container h3{font-size:24px;font-weight:bold;margin-bottom:15px;font-family:inherit}
        .password-container p{font-size:16px;font-family:inherit;max-width:450px;margin-bottom:25px}
        .post-password-form{display:flex;flex-direction:column;align-items:center;width:100%;max-width:300px}
        .post-password-form input[type="password"]{width:100%;padding:12px;margin-bottom:15px;background-color:#ffffff;border:1px solid #dddddd;border-radius:4px;font-family:inherit;font-size:16px}
        .post-password-form input[type="submit"]{width:100%;padding:12px;background-color:#2271b1;color:#fff;border:none;border-radius:4px;font-family:inherit;font-size:16px;cursor:pointer;transition:all 0.3s ease}
        .post-password-form input[type="submit"]:hover{background-color:#135e96}
        @media only screen and (max-width:768px){.password-container p{font-size:14px}.post-password-form input[type="password"],.post-password-form input[type="submit"]{font-size:14px;padding:10px}}
        @media only screen and (max-width:480px){.password-container p{font-size:13px}.post-password-form input[type="password"],.post-password-form input[type="submit"]{font-size:13px;padding:8px}}
        </style>';
    }
}
add_action('wp_head', 'custom_password_form_css');
				
			
    • In the snippet settings:
      • Set the scope to “Only on the front-end”
      • Check the “Active” option
      • Set priority to 10
      • Click “Save Changes and Activate”

How It Works

This snippet does two key things:

  • PHP Function (custom_password_protected_form): Replaces the default WordPress password form with a custom HTML structure featuring:
    • A clear heading
    • A descriptive paragraph
    • A vertically aligned form with password field and submit button

 

  • CSS Function (custom_password_form_css): Adds styling to make the form look professional and match modern web design standards. The CSS is:
    • Loaded conditionally only on password-protected pages
    • Minified to reduce file size
    • Fully responsive for all device sizes

Customization Options

You can easily customize this snippet to match your site’s design:

Change the Text

Edit the heading and paragraph text in the $output  variable:

				
					$output = '<div class="password-container">
    <h3>Members Only</h3>
    <p>This exclusive content is available to members only. Please enter your password:</p>
    <!-- rest of the code -->
				
			

Modify the Colors

Change the background, text, and button colors by editing the CSS variables in the custom_password_form_css function:

				
					.password-container{background-color:#your-bg-color}
.post-password-form input[type="submit"]{background-color:#your-button-color}
.post-password-form input[type="submit"]:hover{background-color:#your-hover-color}
				
			

Adjust the Height

If you need a taller or shorter form container, modify the height value:

				
					.password-container{height:500px} /* Change from 400px to your preferred height */
				
			

Performance Considerations

This solution is optimized for performance in several ways:

  1. Conditional Loading: CSS is only loaded on password-protected pages
  2. Minified CSS: Reduces file size and improves loading speed
  3. No Additional HTTP Requests: No external CSS files needed
  4. Lightweight Code: Minimal impact on page load time


With this simple code snippet, you can transform the default WordPress password form into a professional, user-friendly, and stylish interface that matches your website’s design. No plugins required (except Code Snippets for easy implementation), no performance issues, and fully customizable to suit your needs.
This approach works with any WordPress theme and is easy to update or modify as your site evolves. Enhance your visitors’ experience with a password form that looks as good as the rest of your site!

Need help with this solution or looking for custom development?

Visit my Stay In Touch page to connect and discuss your project. Discover a range of web development services and specialized WordPress solutions tailored to your needs. Let's work together to enhance your digital presence.