Reputation: 13
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?
Upvotes: 0
Views: 64
Reputation: 428
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>
Upvotes: 1