• Resolved Kahil

    (@kahil)


    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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Kahil

    (@kahil)

    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!

    @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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hide sidebar content if page is password protected?’ is closed to new replies.