0

I have created password form where visitors can access any password protected post with belonging password. Basically you enter unique password and you are redirected to a post which is protected with given password. Everything works fine (with help from s_ha_dum), except one thing - you have to enter password twice.

functions.php :

function doPasswordStuff(){
    if(isset($_POST['homepagepassword'])){
        global $wpdb;
        $post_password = trim($_POST['passwordfield']);
        $post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %s", $post_password) );
        if (!empty($post_id)) {
                wp_redirect(get_permalink($post_id));
                exit();
        } else {
            // oh dear, there isnt a post with this 'password', put a redirect to a fallback here
            wp_redirect(xxx');
            exit();
        }
        wp_reset_query();
    }
}
add_action('template_redirect','doPasswordStuff');

Do you have any idea how to do this without entering password twice?

  • 1
    Well, the first time the password is entered, it is just being used as search criteria to obtain a post ID. Your code then redirects to that post. It's a protected post, so it will ask for a password. These 2 operations are completely separate. I'm not aware of any way to programmatically log in to a protected post in WP. You certainly cannot do it with wp_redirect. – vancoder Mar 21 '13 at 21:12
3

Here ya go:

function dopasswordstuff(){
   if(isset($_POST['homepagepassword'])) {
    global $wpdb;
    $post_password = trim($_POST['passwordfield']);
    $post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %s", $post_password) );
    if (!empty($post_id)) {
      if ( empty( $wp_hasher ) ) {
        require_once( ABSPATH . 'wp-includes/class-phpass.php' );
        // By default, use the portable hash from phpass
        $wp_hasher = new PasswordHash(8, true);
      }
      // 10 days
      setcookie( 'wp-postpass_' . COOKIEHASH, $wp_hasher->HashPassword( stripslashes( $post_password ) ), time() + 864000, COOKIEPATH );
      wp_redirect(get_permalink($post_id));
    }
    exit;
  }
}
add_action('template_redirect','dopasswordstuff');

I cribbed some stuff from the Core post login system and I believe I got it working. The trick is to set the cookie, which required adding this part:

if ( empty( $wp_hasher ) ) {  
     require_once( ABSPATH . 'wp-includes/class-phpass.php' ); 
     // By default, use the portable hash from phpass
     $wp_hasher = new PasswordHash(8, true);
}
// 10 days
setcookie( 'wp-postpass_' . COOKIEHASH, $wp_hasher->HashPassword( stripslashes( $post_password ) ), time() + 864000, COOKIEPATH );
  • I don't even know what to say man, thank you thousand times - this works perfectly. – HunterListInsight Mar 21 '13 at 23:24
  • Pretty impressive. – vancoder Mar 21 '13 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.