I’d like to be able to have a workflow that works like so:
- Create a post and assign it a password
- Have a single-field form on the home page
- User enters password on home page, and is sent to the corresponding post
Does anyone know if this is possible?
Yes, but you’ll need to hold the details of how a password maps to a post.
So:
On the homepage:
Place in functions.php ( create the file if it doesnt already exist and add
<?php
at the top) or in a plugin file: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 ).
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.
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:
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:
In
functions.php
(of course this could be converted to a plugin):