Skip to content

Instantly share code, notes, and snippets.

@bremercreative
Last active February 19, 2024 16:45
Show Gist options
  • Select an option

  • Save bremercreative/147760a0ffa7b1c0ad499df2bddc274a to your computer and use it in GitHub Desktop.

Select an option

Save bremercreative/147760a0ffa7b1c0ad499df2bddc274a to your computer and use it in GitHub Desktop.
voxel - password protected user entries
/////////// Step 1 ///////////
- for your desired post type add the two following custom fields
1. switcher (field key: switcher-pw)
2. text (field key: text-passwort)
/////////// Step 2 ///////////
add the following php code snippets. make sure to change 'liste' according to your custom post type
function add_inline_script_to_footer() {
global $post;
// Check if it's a 'liste' post or the /pw/ page
if (is_singular('liste') || (is_page() && isset($post->post_name) && $post->post_name == 'pw')) {
$post_id = is_singular('liste') ? get_the_ID() : (isset($_GET['post_id']) ? $_GET['post_id'] : '');
?>
<script type="text/javascript">
var myScriptData = {
ajax_url: '<?php echo admin_url('admin-ajax.php'); ?>',
post_id: '<?php echo $post_id; ?>'
};
</script>
<?php
}
}
add_action('wp_footer', 'add_inline_script_to_footer');
function redirect_single_liste_to_pw_page() {
if (is_singular('liste')) {
$post_id = get_the_ID();
// Check the switcher value
$switcher_pw = get_post_meta($post_id, 'switcher-pw', true);
// If switcher is false, no need for password check, return early
if ($switcher_pw == false) {
return;
}
$post_author_id = get_post_field('post_author', $post_id);
$current_user_id = get_current_user_id();
// Retrieve the password set for the post
$stored_password = get_post_meta($post_id, 'text-passwort', true);
// If no password is set, return early
if (empty($stored_password)) {
return;
}
// Check if the current user is the author of the post
if ($current_user_id == $post_author_id) {
// Current user is the author, no need to check password
return;
}
$cookie_name = 'passwordVerified=' . $post_id;
// Proceed with password check if not verified via cookie
if (!isset($_COOKIE['passwordVerified']) || $_COOKIE['passwordVerified'] != $post_id) {
wp_redirect(home_url('/pw/?post_id=' . $post_id));
exit;
}
}
}
add_action('template_redirect', 'redirect_single_liste_to_pw_page');
function check_password_ajax() {
if (!isset($_POST['post_id']) || !isset($_POST['password'])) {
wp_send_json_error(['message' => 'Missing post ID or password']);
return;
}
$post_id = $_POST['post_id'];
$password = $_POST['password'];
session_start();
$stored_password = get_post_meta($post_id, 'text-passwort', true);
if ($password === $stored_password) {
$_SESSION['password_verified_' . $post_id] = true;
wp_send_json_success(['message' => 'Password is correct']);
} else {
wp_send_json_error(['message' => 'Password is incorrect']);
}
}
add_action('wp_ajax_check_password', 'check_password_ajax');
add_action('wp_ajax_nopriv_check_password', 'check_password_ajax');
/////////// Step 3 ///////////
- create a page with /pw/ slug
- inside this page, add the following code into an elementor HTML widget
<script>
document.addEventListener('DOMContentLoaded', function () {
const form = document.getElementById('password-check-form');
const urlParams = new URLSearchParams(window.location.search);
const originalPostId = urlParams.get('post_id');
// console.log("Form script initialized. Post ID:", originalPostId);
form.addEventListener('submit', function (e) {
e.preventDefault();
const passwordInput = document.getElementById('password-input');
const password = passwordInput.value;
let formData = new FormData();
formData.append('action', 'check_password');
formData.append('post_id', originalPostId);
formData.append('password', password);
fetch(myScriptData.ajax_url, {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data.success) {
// console.log("Password verified. Setting cookie and redirecting.");
document.cookie = "passwordVerified=" + originalPostId + "; max-age=60; path=/";
window.location.href = '/?p=' + originalPostId; // Adjust this URL
} else {
// console.log("Password verification failed.");
passwordInput.value = ''; // Clear the input field
alert('Falsches Passwort'); // Display an alert
}
})
.catch(error => {
console.error("Fetch error:", error);
});
});
});
</script>
<form id="password-check-form">
<input type="password" id="password-input" placeholder="Passwort eingeben...">
<button class="pwcheck-submit" type="submit">Abschicken</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment