IBEX
  • Home
  • Tech Department
  • Software Developers
  • Electronic Designers
  • Product Design
☰
  • Home
  • Tech Department
  • Software Developers
  • Electronic Designers
  • Product Design
  • Web & Cloud Development
  • AI Integration
  • Mobile App Development
  • FAQ
  • About Us
  • Contact Us
  • Portfolio
  • Resources
  • Under the Hood
  • News

Cloud Development

Our resources for other developers, designers and engineers.

Cloud Development

Redirect after login

/Wordpress / Login Logout / Redirect after login

Using built in WordPress redirect_to functionality

On a page you want to redirect back to after login
      $redirect_to = home_url(add_query_arg([], $_SERVER['REQUEST_URI']));
      wp_redirect(wp_login_url($redirect_to));
      exit;

If you’re placing the login form of your home page (or some other page to the default)

Redirect wp-login.php
//******************************************************************
//********** REDIRECT WP LOGIN PAGE TO OUR SITE HOME PAGE **********
//******************************************************************
//Fires page : /wp-login.php
//This method keeps the wp-login functionality working, such as redirect_to, but causes the wp-login.php page to redirect to your chosen page with your login form
add_action('login_init', 'ValeBlockWpLoginPage');
function ValeBlockWpLoginPage()
{
  $action = $_REQUEST['action'] ?? 'login';
  $method = $_SERVER['REQUEST_METHOD'] ?? 'GET';

  $allowed_actions =
  [
    'logout',
    'lostpassword',
    'rp',
    'resetpass',
    'register',
    'postpass',
    'confirmaction',
    'reauth',
  ];

  //Allow actual login POSTs
  if ($method === 'POST' && $action === 'login')
    return;

  //Allow other core login-related actions
  if (in_array($action, $allowed_actions, true))
    return;

  //Only block viewing the default login screen
  if ($method === 'GET' && $action === 'login')
  {
    $redirect_to = '';

    if (!empty($_GET['redirect_to']))
    {
      $redirect_to = wp_validate_redirect(
        wp_unslash($_GET['redirect_to']),
        ''
      );
    }

    $target = home_url('/');      //<<<<<<The page with your login form on it

    if ($redirect_to !== '')
    {
      $target = add_query_arg(
      [
        'redirect_to' => rawurlencode($redirect_to)
      ], $target);
    }

    wp_safe_redirect($target);
    exit;
  }
}
On a page you want to redirect back to after login
  $target = home_url(add_query_arg([], $_SERVER['REQUEST_URI']));
  $login_url = add_query_arg(
  [
    'redirect_to' => rawurlencode($target)
  ], home_url('/')
  );
  wp_safe_redirect($login_url);
  exit;
Catch for page with your login form
//********** TEMPLATE PAGE ABOUT TO LOAD **********
add_action("template_redirect", 'mysite_template_redirect');
function mysite_template_redirect()
{ 

  if ( 
    (is_user_logged_in()) && 
    (parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) == '/')  &&    //<<<<Login page
    (!(str_contains($_SERVER["REQUEST_URI"], 'elementor-preview')))   //Don't get in the way of Elementor page editor previewing
  )
  {
    $redirect_to = home_url('/somepage');   //<<<Default page you want to  redirect to after login

    //Check for redirect to previous page after login
    if (!empty($_GET['redirect_to']))
    {
      $redirect_to = wp_validate_redirect(
        wp_unslash($_GET['redirect_to']),
        home_url('/')
      );
    }

    wp_redirect($redirect_to);
    die();
  }

}

open all | close all

❯ Other IBEX Resources

Home | Terms & Conditions | Privacy Policy
© Copyright IBEX Technology Ltd. All rights reserved. IBEX is a registered trademark of IBEX Technology Ltd.
Footer Logo