January 17, 2024

Lock Content in WordPress with a Simple HTML Code

Play Video

Locking content in WordPress is a clever way to engage visitors and control the visibility of specific sections. In this tutorial, we’ll explore how to lock content in WordPress using a simple HTML code. We will use Elementor in this example but it is also adaptable to other page builders.


Prerequisites

Please make sure you have the following installed and activated:


Pros and Cons of Hiding Content via HTML Code

When considering the implementation of content locking via HTML code, it’s important to weigh the benefits and drawbacks. Below is a table outlining the key pros and cons:

Pros Cons
Simple Implementation: HTML-based content locking is relatively easy to implement, especially with page builders like Elementor. Limited Security: This method provides a basic level of security but is not foolproof. Tech-savvy users might bypass it.
Customizability: Offers flexibility in design and functionality. You can tailor the look and behavior to fit your site’s theme. Not SEO-Friendly: Locked content might not be indexed by search engines, which could affect your site’s SEO performance.
Enhanced User Engagement: Can be used to create interactive experiences that encourage users to engage more with your content. Potential Accessibility Issues: Overuse or improper implementation might hinder the accessibility of your website.
No Plugin Dependency: Eliminates the need for additional plugins, reducing the risk of compatibility issues and site slowdown. Maintenance Required: Any changes in your site’s layout or updates might require you to adjust the HTML code accordingly.

Instructions to Lock Content in WordPress with HTML

  1. Navigate to the page you want to edit with Elementor.
    Navigate to the page you want to edit with Elementor
  2. Assign unique IDs to each element, e.g. the container in the Elementor editor (Advanced > CSS ID), like ‘sec1’ and ‘sec2’.
    Assign unique IDs to each element
  3. Insert an HTML widget above these containers.
    Insert an HTML widget above these containers
  4. Paste the provided HTML code into the widget:
    <input type="text" id="passwordField" placeholder="Enter password">
    <script>
        document.getElementById('passwordField').addEventListener('input', function() {
            var value = this.value;
    
            if (value === '') {
                // Set both sec1 and sec2 to display none if passwordField is empty
                document.getElementById('sec1').style.display = 'none';
                document.getElementById('sec2').style.display = 'none';
            } else if (value === 'CA') {
                document.getElementById('sec1').style.display = 'block';
                document.getElementById('sec2').style.display = 'none';
            } else if (value === 'DA') {
                document.getElementById('sec1').style.display = 'none';
                document.getElementById('sec2').style.display = 'block';
            } else {
                document.getElementById('sec1').style.display = 'none';
                document.getElementById('sec2').style.display = 'none';
            }
        });
    </script>
    <style>
        #sec1, #sec2 {
            display: none;
        }
    </style>

    Source: Code Snippets

  5. Modify the IDs in the code to match those assigned to your elements and set custom passwords in the code for each section. These will be used to unlock the content.
    Modify the IDs in the code to match those assigned to your elements and set custom passwords
  6. Save your changes and preview the page to ensure functionality.
    Save your changes and preview the page to ensure functionality

Official Documentation


Lock Content in WordPress with HTML – Conclusion

By following these steps, you can easily set up content locking on your WordPress site. This approach is great for creating interactive experiences, like revealing hidden content after entering a password.

Required Resources

Elementor Logo
Free Options
Credit to Web Squadron
Web Squadron is a popular YouTube channel dedicated to providing informative tutorials, tips, and insights on web development and design. With a strong focus on WordPress and website builders like Bricks Builder, the channel aims to help users of all skill levels, from beginners to advanced developers, create and optimize their websites. By offering easy-to-understand, step-by-step guidance, Web Squadron provides practical and valuable content.
Visit
Lock Content in WordPress with a Simple HTML Code
Welcome back!
Enter your Helwp credentials to sign in.

No Account yet? Sign Up

My Account
Menu
Give Feedback
Describe your feedback *
Rate Helwp
Share
Facebook
Twitter
LinkedIn
Reddit
Email
WhatsApp
Telegram
Pocket
Report
Problem *
Describe the problem
Want us to reply?
Your E-Mail
Affiliate Disclosure

At Helwp, we’re committed to transparency and honesty. Therefore, we want to inform you that some of the links on our website are affiliate links. This means that, at no additional cost to you, we may earn a small commission if you click through and make a purchase.

We only promote products or services that we genuinely believe in. These affiliate commissions help us to maintain the website and continue to provide you with free, high-quality WordPress content.

If you are interested in how you can support us even further, check out our support page.