WordPress.org

Support

Support » How-To and Troubleshooting » How to password protect a post? Not just the content area.

How to password protect a post? Not just the content area.

  • I would like to password protect a post. Specifically a custom post type WooCommerce product page.

    Unfortunately WordPress’s lame pasword protect feature only protects the_content and not the post image or anything else.

    I don’t care if the whole page disappears and just a password login is displayed or the header and footer are still present. I want more than the_content hidden.

    A plugin would work fine or a functions.php edit.

Viewing 10 replies - 1 through 10 (of 10 total)
  • esmi

    @esmi

    Forum Moderator

    WordPress’ “lame password protect feature” works fine in Twenty Eleven, so try asking on http://www.woothemes.com/support/

    esmi. WordPress’s password protection is lame. Lame by definiton: weak; inadequate; unsatisfactory; clumsy:

    It has nothing to do with WooCommerce and Twenty Eleven doesn’t work better than any other theme.

    WordPress only password protects whatever get’s output in php the_content. It doesn’t hide the post title, author or anything else.

    Think about how many posts and pages use custom fields, featured images and more. Yep, all that is visible.

    esmi

    @esmi

    Forum Moderator

    It doesn’t hide the post title, author or anything else.

    That’s covered by the theme.

    I tested with Twenty Eleven to no avail. It still hides only the content. Am I missing something?

    esmi

    @esmi

    Forum Moderator

    Twenty Eleven does not hide the post title, the author etc because that’s how Twenty Eleven is designed to work. You can stop the display of a post at ant point using the post_password_required() function as a display conditional. Eg:

    <?php if( ! post_password_required() ) :
    [ normal display ]
    else :
    [ default message/form on password protected post ]
    endif;?>

    Like every other tool, it’s how and when you use it that counts 😉

    I found a code snippet from a few years ago here that is very similar to your snippet. The difference is it uses an if statement.

    <?php
        if ( !post_password_required() ) {
                echo 'protected stuff';
        }
    ?>

    Warning: I am not a developer but I hack my way through things.

    So basically the concept would be to wrap whatever I want to hide within the snippet correct?

    esmi

    @esmi

    Forum Moderator

    Sorry! My bad! I managed to miss out the if statement in my version (now corrected). But the premise is exactly the same in both versions. Your theme “decides” what content is displayed to a passing visitor from a password protected Post or Page.

    Getting there. I tested the concept and it works. However, I am unsure about how to implement it into my theme without relying on echo.

    Take single.php as an example. Lets assume I want to password protect everything between the header and footer.

    Again, my PHP skills are at the 3rd grade level.

    esmi

    @esmi

    Forum Moderator

    Using your example above, you start the if conditional immediately after the call to header.php and end if immediately before the call to footer.php.

    <?php get_header();
        if ( !post_password_required() ) {
                [the rest of the page & normal Loop goes here]
        }
    get_footer();
    ?>
    Moritz

    @hellomoritzqueisnerde

    You can stop the display of a post at ant point using the post_password_required()

    hi, I have the same problem: basically the woocommerce plugin transforms the shop-page into custom post type, so the password protection in the adminarea doesnt work. So i would like to password protect a “custom post type” in wordpress. As far as I understood it is outside of the_content. ok, the code is already shown above, but where would I have to implement it? Is this depending on my themefiles?
    Any help appreciated. thx.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to password protect a post? Not just the content area.’ is closed to new replies.