Depending on a number of factors, there may be places in your WordPress site where login and logout URLs are directing to the WordPress “backend” wp-login.php. These can be in the comments.php template or other places in your theme. Also, various plugins such as forums will utilize these urls.
This article will explain a simple way of redirecting these URLs to the login and registration pages set in WP-Members.
This process makes use of some little known and little used WordPress filters, login_url, logout_url, and register_url. These can be used to direct any call to wp_login_url(), wp_logout_url(), or wp_registration_url() to the locations you want to direct to.
logout_url
First, a logout filter. The logout_url hook will allow you to filter the url the user is directed to when they logout. With WP-Members, if you direct to any page in the site with the querystring ?a=logout the logout function will be executed. (If you want the user to be redirected to a specific URL AFTER they logout, that is something for the wpmem_logout_redirect filter.)
A couple notes about the URL you redirect to: It should be obvious that you’ll need to make this appropriate to your domain. Also, I set this example based on WordPress being located in the root of your site. If you are running WP in a subdirectory you might have to adjust accordingly.
add_filter( 'logout_url', function( $url ) {
return 'https://yourdomain.com/?a=logout';
});
login_url
The login redirect is similar, but using a different hook: login_url. Set the URL to whatever page you want users to login in on. That could be a page that you’ve set up with the WP-Members login page shortcode, or really any other page that has login on it. Set the URL according to your specific need (i.e. the url of your login page). This code will also provide a redirect link back to the page the user was on when they clicked the login link.
add_filter( 'login_url', function( $url ) {
$login_page = 'https://yourdomain.com/your-login-page/'
$redirect_to = get_permalink();
return add_query_arg( 'redirect_to', $redirect_to, $login_page );
});
If you have set a login page in the plugin’s settings, this snippet is similar to the above but will pick up your login page setting, using the plugin API function wpmem_login_url() to get the login url location and wpmem_current_url() to get the url to redirect back to:
add_filter( 'login_url', function( $url ) {
return add_query_arg( 'redirect_to', wpmem_current_url(), wpmem_login_url() );
});
register_url
This filter can be used to filter the location of the registration form generated by wp_registration_url:
add_filter( 'register_url', function( $url ) {
return 'https://yourdomain.com/your-registration-page';
});
If you have set a registration page in the plugin’s settings and want to redirect to that, here is a slightly different snippet that uses the wpmem_register_url() to get the WP-Members register page setting:
add_filter( 'register_url', function( $url ) {
return wpmem_register_url();
});
Usage
As with any filters, these can be used in your theme’s functions.php file.