After a bit of thinking, I came up with a solution that allows wp-login.php to be redirected for the public/customers, but not for the admin. I did this by getting the referrer URL. Here is my updated code, but I’m open to other ideas.
//Redirect for login from wp-login.php to my-account if not admin
add_action('init', 'prevent_wp_login');
function prevent_wp_login() {
// WP tracks the current page - global the variable to access it
global $pagenow;
// Check if a $_GET['action'] is set, and if so, load it into $action variable
$action = (isset($_GET['action'])) ? $_GET['action'] : '';
//check if we came from the admin page or wp-admin
$refer = urlencode($_SERVER["REQUEST_URI"]);
if (strpos($refer, 'wp-admin') !== false) {
// stay on wp-login.php
} else {
// Check if we're on the login page, and ensure the action is not 'logout'
if( $pagenow == 'wp-login.php' && ( ! $action || ( $action && ! in_array($action, array('logout', 'lostpassword', 'rp', 'resetpass'))))) {
// Load the home page url
// Redirect to the home page
wp_redirect('/my-account/');
// Stop execution to prevent the page loading for any reason
exit();
}
}
}