For testing code snippets.
<?php
function restrict_page_to_user() {
// Replace with the slug or ID of your page
$restricted_page_slug = '/testing-page';
// $restricted_user_id = 1; // Replace with the user ID of the user who should have access
if (is_page($restricted_page_slug) && !is_user_logged_in()) {
// Redirect non-logged-in users to the login page
wp_redirect(wp_login_url(get_permalink()));
exit;
} elseif (is_page($restricted_page_slug)) {
$current_user = wp_get_current_user();
//if ($current_user->ID !== $restricted_user_id) {
// // Redirect other logged-in users to a 403 Forbidden page or homepage
// wp_redirect(home_url('/403-forbidden')); // You can create a custom 403 page or use the homepage
// exit;
//}
echo 'The current logged in user ID is: '.get_current_user_id();
}
}
add_action('template_redirect', 'restrict_page_to_user');
?>