0

im trying to create a user flow where, when a non-logged-in user tries to access my website, I want them to see an entry page with links to the login and registration pages, but I don't want them to access any other page of my website.

I have tried the following solution, which although works, I'm not sure of the reliability of because I read somewhere that this kind of functionality uses something called Sessions. I want to know if my code is sufficient or I need to do something else. Thanks

function custom_restrict_access() {
    if (is_user_logged_in()) {
        return; // Logged-in users can access all pages
    }

    $allowed_pages = array('entry', 'login', 'register'); // Add your allowed page slugs here
    $current_page_slug = basename(get_permalink());

    if (!in_array($current_page_slug, $allowed_pages)) {
        if (is_page('entry')) {
            return; // Allow access to the entry page
        } else {
            wp_redirect(home_url('entry')); // Redirect non-logged-in users to the entry page
            exit;
        }
    }
}
add_action('template_redirect', 'custom_restrict_access');

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.