First word of advise, do not modify WordPress core files ! It is bad practice and all your changes will be deleted on the next WordPress update. Only make changes to the wp-content folder.
I believe you could use javascript and cookies to save the the last page visited before your user logged in.
I have had a similar issue recently on a shop where I wanted the users to be able to go back to the latest product archive visited by clicking a back button even after navigating through single products and related products etc…
Below is the js I used. That would go to your log in page
// Get the previous page visited URL
var prevURL = document.referrer;
// Function to set the cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// Now set the cookie with this URL
setCookie("url", prevURL, 1);
// Function to read cookies
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
// Read the cookie and redirect to the URL on click
var url = readCookie('url');
jQuery('a#backToShop').on('click', function(e){
e.preventDefault();
window.open(url , "_self");
});
You might want to change that last jQuery “click” function for something appropriate to the behavior needed, probably an action in your functions.php of your child theme to redirect after wp_login
Hope this helps !