0

I have tried various unsuccessful approaches and would like to know if anyone has done this before.

I have a custom post type which will produce Public pages. However, I want that certain information can only be accessed by typing in a passcode.

What I want is: User types in code into form field, If it matches the code stored in the database (meta value in CPT) then private content will be displayed, If it doesn't match then it won't display.

I tried using wp_localize_script() but is doesn't seem to work on the front end. Thought about using session data but that doesn't seem like the best solution. My AJAX is pretty weak so that isn't going well either.

Any thoughts?

1
  • Welcome to SO. Show us what you've tried! :-) SO isn't a code writing service, and you'll get a better response here if you show us the code you've tried, describe why you tried that, what happened, etc. Check out the how to ask, and how to create a minimal, complete, and verifiable example guides. Commented Mar 19, 2020 at 0:05

1 Answer 1

1

Put this in a PHP file called testpage.php and see if it works. It uses PHP sessions ($_SESSION):

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<?php if ($_SESSION["authorized"] == true || $_POST["SecretCode"] == "12345") {
    $_SESSION["authorized"] = true;
    ?>
    …
    …
    …
    TOP SECRET CONTENT
    …
    …
    …
<?php } else if (!empty($_POST["SecretCode"])) { ?>
    Invalid Secret Code
<?php } else { ?>
    <button>Test Secret Code</button>
    <input type="text" id="code"/>
    <script>
        var PostPackage = {
            SecretCode: document.getElementById("code").value
        };
        var CallbackFunction = function () {
            location.reload();
        };
        var OnClickFunction = function () {
            $.post("testpage.php", PostPackage, CallbackFunction);
        };
        $("button").click(OnClickFunction);
    </script>
<?php } ?>
</body>
</html>

Not the answer you're looking for? Browse other questions tagged or ask your own question.