To redirect users to a custom URL after they log in to your WordPress site, you can use the wp_login
action hook and the wp_redirect()
function. The following code snippet will redirect users to the homepage of your site after they log in:
function wpsnippets_login_redirect() {
wp_redirect( home_url() );
exit();
}
add_action( 'wp_login', 'wpsnippets_login_redirect' );
This code uses the home_url()
function to get the URL of your site’s homepage, and then redirects the user to that URL using the wp_redirect()
function. The exit()
function is called immediately after the redirect to ensure that no further code is executed.
You can customize the redirect URL by replacing home_url()
with the URL of your choice. For example, if you want to redirect users to a custom page after they log in, you can use the following code instead:
function wpsnippets_login_redirect() {
wp_redirect( home_url( '/custom-page' ) );
exit();
}
add_action( 'wp_login', 'wpsnippets_login_redirect' );
Replace /custom-page
with the URL of the page you want to redirect users to.