Advertisement
vapvarun

Secure Login Redirection: Redirect wp-login.php to /login with Elementor Compatibility

Dec 12th, 2023 (edited)
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | Software | 0 0
  1. // Redirect wp-login.php to the custom login page
  2. function wbcom_custom_login_redirect($redirect_to, $request, $user) {
  3.     // Is there a user to check?
  4.     if (isset($user->roles) && is_array($user->roles) && in_array('subscriber', $user->roles)) {
  5.         // Redirect them to the default place
  6.         return home_url('/login'); // Updated to use /login
  7.     } else {
  8.         return $redirect_to;
  9.     }
  10. }
  11. add_filter('login_redirect', 'wbcom_custom_login_redirect', 10, 3);
  12.  
  13. // Redirect non-logged-in users accessing wp-login.php
  14. function wbcom_redirect_non_logged_in_users() {
  15.     if (!is_user_logged_in() && strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false && !isset($_POST['wp-submit']) && !defined('DOING_AJAX')) {
  16.         $custom_login_url = home_url('/login'); // Updated to use /login
  17.         wp_redirect($custom_login_url);
  18.         exit;
  19.     }
  20. }
  21. add_action('init', 'wbcom_redirect_non_logged_in_users');
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement