4

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?

2
  • Does this mean that the post is not viewable until the password is entered? Or is it more like a keyword thing? Commented Jul 9, 2011 at 0:37
  • I'd like it to not be viewable until the password is entered. Commented Aug 6, 2011 at 14:21

5 Answers 5

4

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
  • Do you have any insight on how to accomplish the first thing (storing the mappings)? Commented Aug 5, 2011 at 19:35
  • @tnorthcutt The easiest way of doing this is to use a custom field - you'll probably want to add an index to the postmeta table for the meta_value field.
    – anu
    Commented Aug 8, 2011 at 8:34
3
+50

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 ).

1
  • Thanks, Tom. I was able to get this working using the built in password functionality for posts that WordPress provides thanks to your code and suggestion. I'll add an answer with the code I used and accept it to show the full solution, but you get the bounty! Commented Aug 11, 2011 at 16:19
1

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.

1

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.
0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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