Advertisement
vapvarun

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

Aug 30th, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | Software | 0 0
  1. function wbcom_protect_site_for_members_only() {
  2.     // Check if the user is not logged in
  3.     if (!is_user_logged_in()) {
  4.         // Define pages that should be accessible without login
  5.         $accessible_pages = array(
  6.             'login',
  7.             'register',
  8.             'wp-login.php'
  9.         );
  10.  
  11.         // Get the current page slug
  12.         $current_page_slug = basename($_SERVER['REQUEST_URI']);
  13.  
  14.         // Check if the current page is not in the list of accessible pages
  15.         if (!in_array($current_page_slug, $accessible_pages)) {
  16.             // Redirect all non-logged-in users to the login page
  17.             wp_redirect(wp_login_url());
  18.             exit;
  19.         }
  20.     }
  21. }
  22. add_action('template_redirect', 'wbcom_protect_site_for_members_only');
  23.  
  24. // Disable search functionality for non-logged-in users
  25. function wbcom_disable_search_for_non_logged_in_users($query) {
  26.     if (!is_user_logged_in() && $query->is_search()) {
  27.         // Redirect to login page if a non-logged-in user tries to search
  28.         wp_redirect(wp_login_url());
  29.         exit;
  30.     }
  31. }
  32. add_action('pre_get_posts', 'wbcom_disable_search_for_non_logged_in_users');
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement