When a user logs in they are taken to the home page or to the CMS. This can be annoying sometimes.
Example 1) You want a user to fill out a form, but the form requires you to be logged in. You email out the link to the form and the client is required to log in. They are then taken to the CMS or site homepage. Now they have lost the form in the process.
Example 2) You have a Members Only Area that requires being logged in. Perhaps you have BuddyPress, BBPress, a private onine store, or any other part of your site that requires being logged in. You send someone a link to a product, a forum post, or some exclusive content, they click the link, but are asked to login. Once again they are forwarded to the CMS or homepage and have lost the original link.
Solution
Put this code into your Child Theme’s function file. From then on, you will automatically be returned to the page you were trying to access before you were asked to log in.
add_filter( 'login_redirect', function ( $redirect_to, $requested_redirect_to ) {
if ( ! $requested_redirect_to ) {
$redirect_to = wp_get_referer();
}
return $redirect_to;
}, 10, 2 );