Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Does this mean that the post is not viewable until the password is entered? Or is it more like a keyword thing? –  helenhousandi Jul 9 '11 at 0:37
    
I'd like it to not be viewable until the password is entered. –  tnorthcutt Aug 6 '11 at 14:21
add comment

5 Answers

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.
share|improve this answer
    
Do you have any insight on how to accomplish the first thing (storing the mappings)? –  tnorthcutt Aug 5 '11 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 Aug 8 '11 at 8:34
add comment

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

share|improve this answer
    
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! –  tnorthcutt Aug 11 '11 at 16:19
    
Thanks =)!!!!!!! –  Tom J Nowell Aug 12 '11 at 11:08
add comment

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.

share|improve this answer
add comment

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.
share|improve this answer
add comment
up vote 0 down vote accepted

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();
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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