简体   繁体   中英

WordPress : Password Protected Page

I need help with regards to WordPress Password Protect page/s.

After inputting a password, you are usually redirected to the same page where you entered or just showing you the content after inputting password.

What I want is that, after I input the correct password, it redirects to another page of my site. How is that possible? As much as possible, I won't be using any plugins.

Below is my code for functions.php :

<?php
function my_password_form() {
    global $post;
    $label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
    $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
    <p><strong>'.__("Cakes").'</strong></p>
    <p>' . __("This page is password-protected. Please input password below.") . '</p>
    <center><label for="' . $label . '">' . __("Password:") . '<input name="post_password" id="' . $label . '" type="password" size="40"  style="height:20px;"/></label><input type="image" src="images/btn_member02.png" name="Submit" value="' . esc_attr__("Submit") . '"/></center>
    </form>
    ';
    return $output;
}
add_filter('the_password_form', 'my_password_form');
?>

As an example, when I input the correct password on page www.mysite.com/memberlogin , I should be redirected to www.mysite.com/memberpage .

I have write the following code for your problem. Please check the following code which may useful for you ( Write this code in your functions.php ):-

<?php 

    add_action( 'login_form_postpass', 'redirectPasswordProtectedPage' );
    function redirectPasswordProtectedPage()
    {

        if ( ! empty( $_POST['post_password'] ) ) {
        /** @var wpdb $wpdb */
        global $wpdb;
        $query          = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_password = %s", trim($_POST['post_password']) );
        $password_posts = $wpdb->get_row( $query );

        if ( isset( $password_posts ) ) {

            $post_id   = $password_posts->ID;
            wp_safe_redirect(get_permalink(25));
            exit;
        }
      }
    }  
?>

I found a thread and forgot to copy the link but here is the code:

<?php
    echo "<script type='text/javascript'>\nwindow.location = 'http://www.google.com'</script>";
?>

I placed that code in my password protected page. After inputting the password, it redirects me to another page - which what I wanted it to do.

Your form action should be wp-login.php?action=postpass , not wp-pass.php

Source: Using Password Protection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM