Is it possible to direct users to a certain post based on a password entered on the home page?

I’d like to be able to have a workflow that works like so:

  1. Create a post and assign it a password
  2. Have a single-field form on the home page
  3. User enters password on home page, and is sent to the corresponding post

Does anyone know if this is possible?

Related posts

Leave a Reply

5 comments

  1. Yes, but you’ll need to hold the details of how a password maps to a post.

    So:

    • store mappings somewhere, easiest as a hash/key-value pair (password->post_id)
    • get password from field
    • determine post_id, and construct the URL
    • use wp_redirect() to redirect the user.
  2. On the homepage:

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

    Place in functions.php ( create the file if it doesnt already exist and add <?php at the top) or in a plugin file:

    function doPasswordStuff(){
        if(isset($_POST['homepagepassword'])){
            $pass = $_POST['passwordfield'];
            $q = new WP_Query( array( 'meta_key' => 'password_value', 'meta_value' => $pass));
            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');
    

    Add a custom field to your post with the key/name password_value and the value being your password.

    If you want to skip this step and use the password WordPress uses to lock the post directly, you will need to use $wpdb and an SQL query, and to hash the password your checking prior.

    edit: I’ve updated this to use the ‘init’ hook, you can put the code in a plugin file instead of functions.php if you want this to be theme independent. ( Although you will still need to put the form markup somewhere ).

  3. I would do that with a plugin instead. The plugin could still share the password with the post and set the cookie appropriately so the page wouldn’t be accessible w/o entering the password.

    A plugin because to have a manual mapping, the lookup which password belongs to which post would be really expensive without such a mapping. Not useful in a central location like the homepage.

  4. Are the passwords you create for each post going to be unique? If not, how will you be able to identify which post a user is looking for?

    Assuming you are keeping each password unique, I would store the password as post-meta. You could create a custom meta box which allows for entry/storage of the password.

    Then, when a user enters the password on the home page:

    1. Sanitize the user input (Duh!)
    2. Run a custom query using WP_Query. In this query you can use the meta_query argument to search for posts that have the password entered by the user.
    3. Load and display the required post.
  5. Thanks to Tom’s help, here’s what I ended up with. This uses the built in functionality for password-protecting a post, so a visitor only has to enter a password on the home page, then they are taken to the post and are able to directly view the post content without re-entering the password.

    On home page:

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

    In functions.php (of course this could be converted to a plugin):

    if(isset($_POST['homepagepassword'])){
    
        global $wpdb;
    
        $post_password = $_POST['passwordfield'];
        $post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %d", $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();
    }