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'm using WooCommerce, and want to be able to password protect products (or preferably whole categories if possible), but without using the Users & Roles functionality build into WP(because I need anonymous guests to be able to enter the password too).

I've tried the standard post password protection build into WP, but all that does is block the product's description from showing up (i.e. the body of the "post"), but it doesn't stop you from seeing the product image, details, etc (and in fact you can still purchase the product too).

I've gone down the road of writing something myself (basically I included a custom attribute in any page I wanted protected, and then customised the page template's PHP code to require a password before displaying the page, but my attempts (so far) aren't working 100% because they screw with the cart/checkout process (I think it's interrupting the posted form data or something).

Any ideas?

Thanks Dave

share|improve this question

1 Answer 1

You could try hooking into template_include and showing the user a completely different page containing the login form (without changing the URL) if the post is password protected. Combine that WordPress' built in post password functionality and you have something really close to what you want (blocking an entire page).

You could also use {{insert whatever method you prefer}} to check if a post needs a password protection thing. Custom fields, block a whole category, etc.

Here's a simple example using the template_include filter.

<?php
add_filter('template_include', 'wpse77865_hijack_template');
/**
 * Hooked into `template_redirect`.  Checks to see if we're on a singular page
 * and if it's password protected show the user a completely different page.
 *
 * @param   string $tmp The template
 * @uses    locate_template
 * @return  string
 */
function wpse77865_hijack_template($tmp)
{
    if (
        is_singular() &&
        post_password_required(get_queried_object()) &&
        ($pw = locate_template('password.php'))
    ) {
        // if we're here, we are on a singular page
        // need a password and locate_template actually found
        // password.php in our child or parent theme.
        $tmp = $pw;
    }

    return $tmp;
}

The above will replace the single.php template on posts that require a password (eg. the user hasn't entered one yet) with a template file named password.php in your theme and/or child theme. That template might look something like this (ripped from twenty twelve).

<?php
/**
 * Post password form template.
 *
 * @package WordPress
 */

get_header('password'); ?>

<div id="primary">
    <div id="content" role="main">

        <?php while (have_posts()): the_post(); ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e('Password Required', 'wpse'); ?></h1>
                </header><!-- .entry-header -->

                <div class="entry-content">
                    <?php echo get_the_password_form(); ?>
                </div><!-- .entry-content -->

                </footer><!-- .entry-meta -->
            </article><!-- #post -->

        <?php endwhile; // end of the loop. ?>

    </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer('password'); ?>

As you can see, no hint of the content, just the password form. After the user enters the post password, they'll see the normal page. Not sure if this will mess with the cart or checkout procress, but I'm betting that it won't.

Here's the template_include bit as a plugin.

share|improve this answer

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.