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

    Digital marketing without third-party cookies – new rules
    • Technology
    • Trends

    Digital marketing without third-party cookies – new rules

    February 21, 2024 by createIT
    eCommerce healthcheck
    • Services
    • Trends

    eCommerce healthcheck

    January 24, 2024 by createIT
    Live Visitor Count in WooCommerce with SSE
    • Dev Tips and Tricks

    Live Visitor Count in WooCommerce with SSE

    December 12, 2023 by createIT
    Calculate shipping costs programmatically in WooCommerce
    • Dev Tips and Tricks

    Calculate shipping costs programmatically in WooCommerce

    December 11, 2023 by createIT
    Designing a cookie consent modal certified by TCF IAB
    • Dev Tips and Tricks

    Designing a cookie consent modal certified by TCF IAB

    December 7, 2023 by createIT
    Understanding the IAB’s Global Vendor List (GVL)
    • Dev Tips and Tricks

    Understanding the IAB’s Global Vendor List (GVL)

    December 6, 2023 by createIT
    Effortlessly transform JSON data into a clear schema
    • Uncategorized

    Effortlessly transform JSON data into a clear schema

    December 5, 2023 by createIT
    Anatomy of WordPress malware: a technical deep dive
    • Dev Tips and Tricks
    • Technology

    Anatomy of WordPress malware: a technical deep dive

    December 4, 2023 by createIT
    Initializing CMP with the Stub Script and cmpapi.js
    • Dev Tips and Tricks

    Initializing CMP with the Stub Script and cmpapi.js

    November 29, 2023 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