0

My idea is that the visitor enters the password on a page and than the wordpress post with belonging password load if there is one.

Here is the code:

Form on page:

<form method="post" action="">
  <input type="password" name="passwordfield">
  <input type="hidden" name="homepagepassword" value="1">
  <input type="submit" value="Submit">
</form>

Function.php:

function dopasswordstuff(){
    if(isset($_POST['homepagepassword'])){
    $post_password = $_POST['passwordfield'];
    global $wpdb;
    $post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %s", $post_password) );
    $q = new WP_Query( 'p=$post_id' );
    if($q->have_posts()){
        while($q->have_posts()){
            $q->the_post();
            wp_redirect( get_permalink ());
            die();
        }
    } else {
        // oh dear, there isnt a post with this 'password', put a redirect to a fallback here
        wp_redirect('http://www.google.com');
        die();
    }
    wp_reset_query();
    }
}
add_action('init','dopasswordstuff');

No matter what I do, and no matter what password I enter, the function just directs me to the first post on my blog. Can anybody shed some light on this?

3
  • 1
    Why don't you use the built-in password protection? – fuxia Mar 21 '13 at 15:21
  • Built-in password protection is not working the way I would like. I'm trying to direct users to a certain post (via form on a page) based on a password entered in post settings. eg. if someone enters "pass1" he will be directed to a post which has set password "pass". – HunterListInsight Mar 21 '13 at 15:30
  • 1
    You can do that with the built-in password protection. – fuxia Mar 21 '13 at 15:32
0

Here is all you need to make this work, plus your actual form of course:

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;
   }
}
add_action('template_redirect','dopasswordstuff');

I simplified your function considerably by leveraging the fact that get_permalink will accept a post ID a parameter.

I hooked to template_redirect instead of init which seemed more appropriate.

2
  • WOW, hell yeah you're awesome! There is still one issue - it seems this function doesn't register that you already entered password so once you are redirected to post you have to re-enter password. – HunterListInsight Mar 21 '13 at 19:16
  • I will see what I can do. – s_ha_dum Mar 21 '13 at 22:32
0

I think I found the answer with this code which uses cookies (I didn't write it!), however if someone types in the incorrect password I get the white screen of death so if anyone knows how to add the code back in to give a redirect that would be awesome.

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');
1
  • Please format your code correctly and if you didn't write that code then please properly cite the code's author. – s_ha_dum May 8 '14 at 21:15

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.