WP Password protected page - createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.
    Tags

    WP Password protected page

    WP Password protected page

    CHALLENGE: share secret content protected by a password form

    SOLUTION: create a new WordPress page with Visibility: Password protected

    We often need a simple and straightforward way to share content only with a specific group of people. By default, content is restricted, only visitors with password can access it. WordPress CMS has this feature built in – when creating a new page, go to the ‘Publish’ metabox on the right and change the ‘Visibility’ parameter.

    Password template

    Your WordPress theme might already include the defined special PHP template for handling the wp password form. If not, here is sample code. The file should be added in your child theme / or parent theme directory.

    <?php
    /**
     * // tpl-password-protected.php
     * Template Name: Password protected content
     */
    get_header(); ?>
    <?php
    global $post;
    $protect_content = true;
    if (!post_password_required($post)) {
        // if there is no password required
        // display the content
        $protect_content = false;
    } else {
        echo '<div class="container py-5">';
        echo get_the_password_form();
        echo '</div>';
    }
    if($protect_content){
        die();
    }
    ?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <?php
            while (have_posts()) :
                the_post(); ?>
                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php
                    wp_link_pages(
                        array(
                            'before' => '<div class="page-links">' . __('Pages:', 'storefront'),
                            'after' => '</div>',
                        )
                    );
                    ?>
                </div><!-- .entry-content -->
            <?php
            endwhile; // End of the loop.
            ?>
        </main><!-- #main -->
    </div><!-- #primary -->
    <?php
    get_footer();

    Edit page Attributes

    The last step will be to enable the proper Template using the ‘Page Attributes’ metabox. The ‘Password protected content’ template should be chosen from select. Make sure to Update/Save Page after the change. That’s it, now your page content is protected. Text will not be visible in search results. On page visit, there will be a message: “This content is password protected. To view it please enter your password below:” and a form with password input will be provided. Filling in the password is required to see restricted content.

    WordPress dashboard publication options
    Password protection window with a box to type in the password

    Follow us for more useful tips and guidelines.

    Comments
    0 response

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    Automating stock and price updates in WooCommerce
    • Dev Tips and Tricks

    Automating stock and price updates in WooCommerce

    September 23, 2024 by createIT
    Integrating advanced subscription features in WooCommerce
    • Dev Tips and Tricks

    Integrating advanced subscription features in WooCommerce

    September 16, 2024 by createIT
    Fetching Time records from ActiveCollab API
    • Dev Tips and Tricks

    Fetching Time records from ActiveCollab API

    September 9, 2024 by createIT
    Docker Compose for PrestaShop
    • Dev Tips and Tricks

    Docker Compose for PrestaShop

    September 2, 2024 by createIT
    WordPress wizard in admin – step by step
    • Dev Tips and Tricks

    WordPress wizard in admin – step by step

    August 29, 2024 by createIT
    Order Status Sync between PrestaShop and External APIs
    • Dev Tips and Tricks

    Order Status Sync between PrestaShop and External APIs

    August 26, 2024 by createIT
    What is PHP used for in web development 
    • Dev Tips and Tricks

    What is PHP used for in web development 

    August 22, 2024 by createIT
    Automating WooCommerce product availability date
    • Dev Tips and Tricks

    Automating WooCommerce product availability date

    August 15, 2024 by createIT
    WP Quiz Adventure – FAQ
    • Dev Tips and Tricks

    WP Quiz Adventure – FAQ

    August 12, 2024 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us