WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hide sidebar content if page is password protected? (3 posts)

  1. Kahil
    Member
    Posted 2 years ago #

    Ok, so I have some pages where I password protect them using the built in system for doing so. I would like to have certain widgets/sections of the sidebar to also be hidden until the password is entered. Does anyone know of a way to achieve this? Maybe some sort of If/Then statement/code?

    Any help would be greatly appreciated!

  2. Kahil
    Member
    Posted 2 years ago #

    I figured it out!!!

    So I wanted to hide a specific custom field that was being called in my sidebar on password protected pages. That way the info is hidden until users entered the password. By default, when you password protect a page or post, only the content of the main section is hidden. Here's how I got around that...

    First, password protect your page.

    Next, instead of using a widget to call a custom field value, use a query. For me, I have the content at the top of my sidebar. If you have it elsewhere, this solution probably won't work for you. I edited my sidebar template and placed the query above the section that is there for widgets.

    To query a specific custom field, the code is as follows:

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'custom-field-name', true);
    wp_reset_query();
    ?>

    To hide other content until a page's or post's password is entered, the code is as follows:

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

    To get what I wanted to achieve, I simply combined the two codes to get the following result for my sidebar template. And it works!

    <div id="sidebar">
    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    if ( !post_password_required() ) {
    echo get_post_meta($postid, 'progress', true);}
    wp_reset_query();
    ?>
        <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Default-Sidebar") ) : ?>
    
        <?php endif; ?>
    </div>
    <!-- sidebar END here -->

    Hope this helps anyone else looking to get the same or similar result!

  3. ryanITLab
    Member
    Posted 2 years ago #

    @Kahil - thanks dude - you are a life saver!!

    This is exactly what I needed and it worked right away - saved me a bunch of time

Topic Closed

This topic has been closed to new replies.

About this Topic