Hide Custom Widget/Sidebar on Password Protected Pages

Our PPWP Pro plugin facilitates comprehensive protection for unlimited pages, posts, and custom post types with ease. However, the custom widget or sidebar will not be hidden by default.

Simply put, there are 2 ways to hide the custom widget/sidebar on your password protected pages:

  1. Download and install Insert PHP Code Snippet from the WordPress repository.
    This plugin allows you to insert any arbitrary Text/HTML and run PHP code or shortcode directly on the widget content.

    Or else, you can use custom code to run PHP code in WordPress widget as well. All you have to do is add this code to your (child) theme’s functions.php file.

    function widget_text_exec_php( $widget_text ) {
        if( strpos( $widget_text, '<' . '?' ) !== false ) {
            ob_start();
            eval( '?>' . $widget_text );
            $widget_text = ob_get_contents();
            ob_end_clean();
        }
        return $widget_text;
    }
    add_filter( 'widget_text', 'widget_text_exec_php', 99 );
  2. Under your HTML/Text or PHP code widget, add the following conditional logic to your HTML content, i.e. buttons and text, accordingly
<?php if (!post_password_required()) : ?>

    <button class="us-button us-button-default us-button-small">Default</button>

<?php endif; ?>

    <button class="us-button us-button-primary us-button-small">Primary</button>

 

Lasted updated on September 24, 2020