vapvarun icon

Site members-only and ensure that only logged-in users can access content beyond the login and regis

vapvarun | PRO | 08/30/24 11:05:09 AM UTC | 0 ⭐ | 462 👁️ | Never ⏰ | []
PHP |

1.15 KB

|

Software

|

0 👍

/

0 👎

function wbcom_protect_site_for_members_only() {
    // Check if the user is not logged in
    if (!is_user_logged_in()) {
        // Define pages that should be accessible without login
        $accessible_pages = array(
            'login',
            'register',
            'wp-login.php'
        );
 
        // Get the current page slug
        $current_page_slug = basename($_SERVER['REQUEST_URI']);
 
        // Check if the current page is not in the list of accessible pages
        if (!in_array($current_page_slug, $accessible_pages)) {
            // Redirect all non-logged-in users to the login page
            wp_redirect(wp_login_url());
            exit;
        }
    }
}
add_action('template_redirect', 'wbcom_protect_site_for_members_only');
 
// Disable search functionality for non-logged-in users
function wbcom_disable_search_for_non_logged_in_users($query) {
    if (!is_user_logged_in() && $query->is_search()) {
        // Redirect to login page if a non-logged-in user tries to search
        wp_redirect(wp_login_url());
        exit;
    }
}
add_action('pre_get_posts', 'wbcom_disable_search_for_non_logged_in_users');
 

Comments