WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Password form not showing on protected page template, content just disappears (6 posts)

  1. dave
    Member
    Posted 1 year ago #

    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!

  2. dave
    Member
    Posted 1 year ago #

    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!

  3. dave
    Member
    Posted 1 year ago #

    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!

  4. endlessqiong
    Member
    Posted 1 year ago #

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

  5. manthono
    Member
    Posted 11 months ago #

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

  6. Rebecca Putinski
    Member
    Posted 10 months ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic