@justmeandmyself
Hi, thanks for using Force Login!
I would like to know, if there is any reason to implement the parameters in the function when they aren’t used?
In my code example, the $redirect
parameter is being used, and since it’s the second parameter passed through the login_url
hook –?both parameters were required.
Further I would like to ask if it would be safe to change [the] snippet?
Yes, you may certainly use your snippet, but it will not include the path to redirect to on login, if supplied.
For example, if you wanted visitors to be redirected back to the page they tried to visit while logged-out,?it won’t redirect the user after successful login because your custom login URL will remove it.
Also, if you don’t use the two parameters in your custom filter, you will need to update your filter to this:
function my_login_page() {
return site_url('/login/');
}
add_filter( 'login_url', 'my_login_page' );
Lastly, you might want to try the following code snippet instead of my original one. This code will only add the redirect_to
query string to the login URL if it’s not empty:
/**
* Custom Login URL.
*
* @param string $login_url The login URL. Not HTML-encoded.
* @param string $redirect The path to redirect to on login, if supplied.
*
* @return string
*/
function my_login_page( $login_url, $redirect ){
// This will append /custom-login/ to you main site URL as configured in general settings (ie https://domain.com/custom-login/)
$login_url = site_url( '/custom-login/', 'login' );
if ( ! empty( $redirect ) ) {
$login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_url );
}
return $login_url;
}
add_filter( 'login_url', 'my_login_page', 10, 2 );
Good luck!