• Resolved dave

    (@mozdzanowski)


    I’m trying to password protect a page with a custom template. I used the

    <?php
      if ( !post_password_required() ) {
        ##the password-tested content##
      }
    ?>

    around the content I want protected, as suggested in other posts.

    The title is switched to “Protected: #title”, but my content only disappears and no password input form shows up. Any ideas how to get this to work? I feel like it’s almost there, but I’m not sure how to get it 100% working. Thanks in advance for any ideas!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter dave

    (@mozdzanowski)

    I ended up using this code:

    <?php
    // Globalize $post
    global $post;
    // Test for password-protected page
    // Returns true if post is password-protected
    // and if the password doesn't match cookie
    if ( post_password_required( $post ) ) {
        ?>
        <p>
        CUSTOM CONTENT THAT DISPLAYS ONLY WHEN PAGE IS PASSWORD PROTECTED
        GOES HERE
        </p>
        <?php
    } else {
        // Page isn't password-protected
        ?>

    because I realized the code doesn’t automatically include the password input field.

    My question now is: What is the code to add the proper password input field?

    Thanks!

    Thread Starter dave

    (@mozdzanowski)

    Ok, so after some more research and tweaking, I ended up with this code:

    <p>
        <form action="<?php get_option('siteurl'); ?>/wp-login.php?action=postpass" method="post">
        <p>This page is password protected. Please enter your client password below:</p>
        <p><input name="post_password" id="<?php $label ?>" type="password" size="20" /><input type="submit" name="Submit" value="Submit" /></p>
        </form>
        </p>

    in the area where “Custom content that displays only when page is password protected goes here” is. The main thing I was looking for was the form action that would ask for the correct password (“postpass”).

    Hope this helps someone!

    Hi,thank you for sharing. But where I should put this code? In my template page?

    I think it’s easier if you use this
    <?php echo get_the_password_form(); ?>

    Note that the password form uses <siteurl>/wp-pass.php now, instead of wp-login.php?action=postpass.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Password form not showing on protected page template, content just disappears’ is closed to new replies.