Redirecting Users to Custom Pages After WordPress Login
When building a customized WordPress theme or developing an alternate dashboard, the default behavior of redirecting authenticated users to /wp-admin/ is often undesirable. The goal is typically to direct them straight to a specific resource, such as news.php, upon successful authentication. While plugins exist to handle this logic, implementing it manually within functions.php offers granular control and can reduce dependency on third-party code.
The core mechanism for managing post-login redirects in WordPress resides in the wp_signon action hook. This hook fires immediately after a user successfully authenticates via username/password or any other method that bypasses standard form validation. It is distinct from the login_redirect filter, which was introduced later and offers slightly different behavior regarding session management.
To implement a manual redirect, you need to hook into wp_signon and check if the current user is authenticated before attempting any redirection logic. Inside your theme's functions.php file or a custom plugin, add the following code:
add_action( 'wp_signon', function( $user ) {
// Ensure we are redirecting only when a successful login occurs
if ( isset( $_GET['redirect_to'] ) ) {
return; // Let default behavior handle it if a specific redirect was requested via URL
}
// Define your custom destination.
// Note: 'news.php' is relative to the site root.
// If you are on a subdirectory, ensure the path is accurate.
$custom_path = '/news.php';
// Check if the user is already logged in (double-checking context)
if ( is_user_logged_in() ) {
// Use wp_redirect to perform the HTTP redirect immediately
// This must be done before any output is sent
wp_redirect( site_url( $custom_path ) );
exit;
}
}, 10, 1 );
This snippet attaches a callback function to the wp_signon action with a priority of 10. The first parameter, $user, contains the authenticated user object. The logic checks for existing redirect parameters in the GET request to avoid overriding explicit URL requests made during the login process itself. By using site_url() combined with the relative path, you ensure the redirect works correctly regardless of whether your site is installed at the root or within a subdirectory.
A critical technical detail involves the order of execution. WordPress processes actions sequentially; if any other code attempts to output headers or content before this hook runs, you will encounter a "headers already sent" error. Therefore, this logic must be placed near the top of your functions.php file or loaded early via an init hook. Additionally, the exit statement is mandatory after wp_redirect() to prevent the rest of the script from executing and potentially interfering with the session state.
One significant trade-off with manual redirection is the handling of logged_out parameters. By default, WordPress attempts to maintain session continuity for logged-out users when they return to a page. When you force a redirect to news.php, you must ensure that the target page handles the lack of admin context gracefully. If news.php relies on functions only available within the admin area (like get_admin_url()), you will encounter fatal errors because the user is technically logged into WordPress but not the dashboard.
Another consideration is the distinction between wp_signon and login_redirect. The latter filter allows you to modify the redirect URL based on the user's origin, which can be useful for returning users to their last visited page before logging in. However, if your requirement is strictly to send every authenticated user to a specific landing page regardless of their entry point, the wp_signon approach described here is more direct and less prone to creating redirect loops.
If you are managing a large-scale application or require complex permission-based routing, consider how this manual override interacts with other security plugins that might monitor for unauthorized redirects. While using a plugin is often simpler, understanding these underlying hooks ensures you can troubleshoot authentication flows effectively without relying on black-box solutions.
