• Resolved inveencible

    (@inveencible)


    I have a password protected page (Page X), but I need the password form that grants access to Page X to live on the home page.

    With the password form on the home page, I want the user to be able to enter the correct password directly. If correct, I want the user to be redirected to the password protected Page X.

    Is this possible to implement? Normally, once a user presses on a link that is password protected, they are redirected to the password form that is its own separate page. But I want to cut out this intermediary password form page, and place the form directly on the home page. If you have any tips, please let me know.

    I tried using the get_the_password_form function below (from wp-includes folder) and passing in the page/post ID. It does return the proper HTML code for a form, and a form is displayed. However, it does not function correctly: does nothing despite entering in the correct password.

    • This topic was modified 7 years, 4 months ago by inveencible.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • I was able to do as req with the following. This snippet assumes Page X‘s ID is 123:

    On the homepage, entered wherever:

    [custom_password_form for=123]

    then in functions.php, something like

    add_shortcode( 'custom_password_form', 'davidsword_custom_password_form' );
    function davidsword_custom_password_form($atts) {
        global $post;
        
        // get PAGE X id from shortcode
        $a = shortcode_atts( array(
            'for' => false
        ), $atts );
        
        // just incase of misuse
        if (!$a['for'])
        	return "<p>please add a <code>for={ID}</code> value in your <code>[custom_password_form]</code> shortcode</p>";
        
        // if there's a requirment for a password for PAGE X, show form <code>id=&quot;pwbox-#</code> is important
        if ( post_password_required( $a['for'] )) {
            return '
            <form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
                <p>Password for <strong>'.get_post($a['for'])->post_title.'</strong></p>
                <input name="post_password" id="pwbox-' . $a['for'] . '" type="password" />
                <input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
            </form>';
        } 
        // if the password was entered correctly for PAGE X, redirect via JAVASCRIPT
        else {
            return '<script type="text/javascript">window.location = "'.get_permalink( $a['for'] ).'"</script>';
        }
    }
    

    one might argue that redirects with Javascript aren’t wise – so adding some html instructions/link within a <noscript> after the redirect, or an improper <meta> redirect tag (thats technically wrong, but would still work), would be more universal.

    • This reply was modified 7 years, 4 months ago by David Sword. Reason: added notes about noscript
    Thread Starter inveencible

    (@inveencible)

    You’re the best. Thank you!

    markimark3269

    (@markimark3269)

    @davidsword
    Hi David,
    I have a similar task to achieve but I have not only 1 password protected page but a few of them (all nextgen pages with client galleries, all with password. What I need to achieve is to have a password page, where people type in a password and that opens the page with the gallery. Understand what I mean? We have a photography studio and we offer those client galleries, generated with nextgen and they all have a password. I have to find an easy way to access the gallery.

    Next for any help.

    Mark

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to implement a custom password field that redirects to another page?’ is closed to new replies.