Redirect after Login on WordPress

PhpWordpressRedirectWordpress Theming

Php Problem Overview


I'm creating a customized WordPress theme based on an existing site.

I want to use an alternate dashboard which I have created.

How can I have the user directed to 'news.php' after login instead of '/wp-admin/' ?

--

EDIT: Have a working Plug-in for this but the bounty is still availible for anyone who can find a manual way to do this through functions.php, as it would be more secure then using a 3rd party plug-in.

Php Solutions


Solution 1 - Php

This should solve your problem. Adapted from an answer found here.

Add the following snippet of code in the functions.php file of your theme:

function admin_default_page() {
  return '/new-dashboard-url';
}

add_filter('login_redirect', 'admin_default_page');

Solution 2 - Php

The accepted answer is very wrong. One should never be modifying the WordPress Core. Not only will edits be lost at a given update, some changes you make on a whim may compromise other functionality or even endanger the security of your site.

Action Hooks & Filters are included within the core to allow modifying functionality without modifying code.

An example of using the login_redirect filter to redirect certain users can be found here and is a much more robust solution to your problem.

For your specific problem, you want to do this:

function login_redirect( $redirect_to, $request, $user ){
	return home_url('news.php');
}
add_filter( 'login_redirect', 'login_redirect', 10, 3 );

Solution 3 - Php

This may help. Peter's Login Redirect > Redirect users to different locations after logging in and logging out. > Define a set of redirect rules for specific users, users with specific roles, users with specific capabilities, and a blanket rule for all other users. Also, set a redirect URL for post-registration. This is all managed in Settings > Login/logout redirects. > You can use the syntax [variable]username[/variable] in your URLs so that the system will build a dynamic URL upon each login, replacing that text with the user's username. In addition to username, there is "userslug", "homeurl", "siteurl", "postid-23", "http_referer" and you can also add your own custom URL "variables"...

Solution 4 - Php

You can also use the customized link as:

https://example.com/wp-login.php?redirect_to=https://example.com/news.php

Solution 5 - Php

add_action('wp_head','redirect_admin');
function redirect_admin(){
  if(is_admin()){
    wp_redirect(WP_HOME.'/news.php');
    die; // You have to die here
  }
}

Or if you only want to redirect other users:

add_action('wp_head','redirect_admin');
function redirect_admin(){
  if(is_admin()&&!current_user_can('level_10')){
    wp_redirect(WP_HOME.'/news.php');
    die; // You have to die here
  }
}

Solution 6 - Php

If you have php 5.3+, you can use an anonymous function like so:

add_filter( 'login_redirect', function() { return site_url('news'); } );

Solution 7 - Php

The Theme My Login plugin may help - it allows you to redirect users of specific roles to specific pages.

Solution 8 - Php

The accepted answer is clearly not a good answer! It may solve your problem for a while, but what will happen next time you update your WordPress installation? Your core files may get overridden and you will loose all your modifications.

As already stated by others (Dan and Travis answers), the correct answer is to use the login_redirect filter.

Solution 9 - Php

Please try this, it works for any redirection on WordPress

add_filter('woocommerce_login_redirect', 'wc_login_redirect'); 

function wc_login_redirect( $redirect_to ) {

   $redirect_to = 'PUT HERE URL OF THE PAGE';
   return $redirect_to;

}

Solution 10 - Php

// add the code to your theme function.php
//for logout redirection
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_redirect( home_url() );
exit();
}
//for login redirection
add_action('wp_login','auto_redirect_after_login');
function auto_redirect_after_login(){
wp_redirect( home_url() );
exit();
`enter code here`}

Solution 11 - Php

for WooCommerce: redirects for login / logout - working in 2021

add_filter('woocommerce_login_redirect', 'login_redirect');

function login_redirect($redirect_to) {

    return home_url();

}

add_action('wp_logout','logout_redirect');

function logout_redirect(){

    wp_redirect( home_url() );
    
    exit;

}

Solution 12 - Php

Here is a code snippet to redirect user based on user role:

add_filter( 'login_redirect', 'kp_login_redirect', 10, 3 );

function kp_login_redirect( $redirect_to, $request, $user ) {
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		if ( in_array( 'subscriber', $user->roles )) {
			 $redirect_to = home_url("dashboard");
		}
	}
	return $redirect_to;
}

If you are using Network site (multisite) then try this:

add_filter( 'login_redirect', 'kp_login_redirect', 10, 3 );

function kp_login_redirect( $redirect_to, $request, $user ) {
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		if ( in_array( 'subscriber', $user->roles )) {
			 $redirect_to = network_site_url("dashboard");
		}
	}
	return $redirect_to;
}

Solution 13 - Php

// Used theme's functions.php  
add_action('login_form', 'redirect_after_login'); 
function redirect_after_login() 
{     
global $redirect_to; 
if   (!isset($_GET['redirect_to'])) 
{ 
$redirect_to =   get_option('sample-page');
//  sample-page = your page name after site_url
} }

Solution 14 - Php

Based on some of the other answers, I came up with this:

/**
 * Require login on all pages
 */
function so8127453_require_login() {
  if ( ! is_user_logged_in() ) {

    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
    $base = $_SERVER['SERVER_NAME'];
    $uri = $_SERVER['REQUEST_URI'];

    $attempted_accessed_url = $protocol . $base . $uri;
    $login_url = 'https://' . $base . '/wp-login.php?redirect_to=' . $attempted_accessed_url;

    wp_redirect( $login_url );
  }
}
add_action( 'template_redirect', 'so8127453_require_login' );

Redirects all traffic to a login-page. And then to the attempted access URL afterwards.

Solution 15 - Php

I was searching "How to Fix WordPress Login Page Refreshing and Redirecting Issue?" and did not find any good fix. From this Stackoverflow question, I have got my help. I would like to share it with others so that in case they need it, they get the help.

On my website, when I was entering email and password, I was redirecting again and again to wp-admin and asked for passwords. This code helped me to fix the issue:

function admin_default_page() {
  return '/';
}

add_filter('login_redirect', 'admin_default_page');

Solution 16 - Php

Right out of the box I found this which does it for me. You can just replace the argument with a hardcoded site if you like

wp_loginout($_SERVER['REQUEST_URI']); 

Solution 17 - Php

One can use wp_safe_redirect or wp_redirect.

If the home page is your front page, something like this would do the work:

function add_login_check()
{
    if (is_user_logged_in() && is_front_page()) {
            wp_safe_redirect('https://sample.com/dash'); # Using wp_safe_redirect
            exit; 
        }
    }
}

If you know the ID of the page (to check how does one see the ID of the page, see note below), one could use something like

function add_login_check()
{
    if ( is_user_logged_in() && is_page(765)) {
        wp_redirect('https://sample.com/dash'); # Using wp_redirect
        exit;
    }
}

Or

function add_login_check()
{
    if (is_user_logged_in())) {
        if (is_page(765)){ # If the user is in the page with the ID 765
            wp_safe_redirect( get_permalink('234')); # To get the URL of a specific page from the ID, and using wp_redirect
            exit; 
        }
    }
}

Note: In order to check the ID of a page, access the Pages section in the WP Admin Dashboard and scroll on top of the page. See image below (Source)

enter image description here

Alternatively, if one access the page or post, one should also be able to see the page ID on the URL. See image below (Source)

enter image description here

Solution 18 - Php

To globally redirect after successful login, find this code in wp-login.php, under section.

   <form name="loginform" id="loginform" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" method="post">

<input type="hidden" name="redirect_to" value="<?php echo esc_attr($redirect_to); ?>" />

and replace <?php echo esc_attr($redirect_to); ?> with your URL where you want to redirect. The URL must start with http:// and ends on /other wise page redirect to default location.

Do same thing form redirect after registration with in same file but under <form name="registerform"> section.

Solution 19 - Php

The functions.php file doesn't have anything to do with login redirect, what you should be considering it's the wp-login.php file, you can actually change the entire login interface from there, and force users to redirect to your custom pages instead of the /wp-admin/ directory.

Open the file with Notepad if using Windows or any text editor, Prese Ctrl + F (on window) Find "wp-admin/" and change it to the folder you want it to redirect to after login, still on the same file Press Ctrl + F, find "admin_url" and the change the file name, the default file name there is "profile.php"...after just save and give a try.

if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) )
		$redirect_to = admin_url('profile.php');
	wp_safe_redirect($redirect_to);
	exit();

Or you can use the "registration-login plugin" <http://wordpress.org/extend/plugins/registration-login/>;, just simple edit the redirect urls and the links to where you want it to redirect after login, and you've got your very own custom profile.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilip KirkbrideView Question on Stackoverflow
Solution 1 - PhpTravisView Answer on Stackoverflow
Solution 2 - PhpDanView Answer on Stackoverflow
Solution 3 - PhpVasanthan.R.PView Answer on Stackoverflow
Solution 4 - PhpHossein View Answer on Stackoverflow
Solution 5 - PhpLeo JiangView Answer on Stackoverflow
Solution 6 - PhpFarahmandView Answer on Stackoverflow
Solution 7 - PhpgArnView Answer on Stackoverflow
Solution 8 - PhpMikeView Answer on Stackoverflow
Solution 9 - PhpMohammad Altamash AnsariView Answer on Stackoverflow
Solution 10 - PhpLokesh MettaView Answer on Stackoverflow
Solution 11 - PhpAndreea PurtaView Answer on Stackoverflow
Solution 12 - PhpKishor PatidarView Answer on Stackoverflow
Solution 13 - PhpOmprakash PatelView Answer on Stackoverflow
Solution 14 - PhpZethView Answer on Stackoverflow
Solution 15 - PhpHussain AbdullahView Answer on Stackoverflow
Solution 16 - PhpRuneView Answer on Stackoverflow
Solution 17 - PhpGonçalo PeresView Answer on Stackoverflow
Solution 18 - PhpSYED FARHAN KARIMView Answer on Stackoverflow
Solution 19 - PhpCharming PrinceView Answer on Stackoverflow