vapvarun

Redirect wp-login.php to a custom login page, but allow exceptions for SSO/2FA

Sep 30th, 2024 (edited)
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | Software | 0 0
  1. // Redirect wp-login.php to a custom login page, but allow exceptions for SSO/2FA.
  2. add_action('init', 'custom_force_custom_login_page');
  3.  
  4. function custom_force_custom_login_page() {
  5.     // Define the custom login page URL
  6.     $custom_login_page = home_url('/login/'); // Replace '/custom-login/' with your custom login page URL.
  7.  
  8.     // Get the current request
  9.     $current_page = basename($_SERVER['REQUEST_URI']);
  10.  
  11.     // If the current page is wp-login.php, and not performing login-related actions, redirect
  12.     if ($current_page === 'wp-login.php' && !is_user_logged_in()) {
  13.  
  14.         // List of actions that should not trigger the redirect (SSO, 2FA, etc.)
  15.         $allowed_actions = ['logout', 'lostpassword', 'resetpass', 'rp', 'login']; // Add any other actions as needed
  16.  
  17.         // Check if any of the allowed actions are being performed
  18.         $is_allowed_action = false;
  19.         if (isset($_GET['action']) && in_array($_GET['action'], $allowed_actions)) {
  20.             $is_allowed_action = true;
  21.         }
  22.  
  23.         // Allow SSO plugins and 2FA plugins by checking the presence of specific query strings or parameters
  24.         // Modify the conditions based on the specific plugins you use.
  25.         if (isset($_GET['sso']) || isset($_GET['2fa'])) {
  26.             $is_allowed_action = true;
  27.         }
  28.  
  29.         // If not performing allowed actions or plugin-related requests, redirect to the custom login page
  30.         if (!$is_allowed_action) {
  31.             wp_redirect($custom_login_page);
  32.             exit();
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment