WordPress themes and plugins output default logout URLs in many parts of your website like contents, comments, widgets, etc. You might want to replace or customize the output of the default logout URL if you have custom access rules for your WordPress login page.
There’s a filter available in WordPress core called logout_url
to make this possible. You can change your website’s front-end or back-end logout URL output easily by modifying the filter.
HEY! You must have the slug of your custom logout link URL to apply this filter. If your custom logout URL is “YourSite.com/logout/”, then “logout” is the page slug.
Change Default WordPress Logout URL
To customize or change default WordPress logout link output everywhere, use the code snippet:
// Change WordPress logout url with a custom link
add_filter( 'logout_url', 'bydik_change_wp_logout_link', 10, 1 );
function bydik_change_wp_logout_link( $link ) {
return home_url( '/logout/?redirect_to=' ) . get_permalink();
}
You might need to change /logout/
in this code if your logout page slug is different. After this, the default logout links of your site (both front-end & back-end) will be changed to the new one.
If you just want to change the URL on the front-end of your site, use the code snippet:
// Change WordPress front-end logout links
add_filter( 'logout_url', 'bydik_change_frontend_logout_link', 10, 2 );
function bydik_change_frontend_logout_link( $link ) {
if ( ! is_admin() ) {
return home_url( '/logout/?redirect_to=' ) . get_permalink();
}
return $link;
}
HEY! Both functions will redirect users to the page they were in. If you want a homepage redirection, then change get_permalink()
to get_home_url()
in the code.
Adding code snippets for the first time? Don’t know where and how to add code snippets? We have an in-depth article on how to add code snippets to a WordPress site.
Have something to say? Please join in the comments and get a response within a quick time!
Hello there thanks for this snippet. I have a question somehow on multisite it is not logging me out but keeps me logged in to the logout page.
Tested in incognito mode few times and I can replicate it.
Second, I had a question for you: would it be possible to fully customise using elementor and your snippets the behavior of login, logout, reset password etc etc… I’d like to offer to user something a bit more branded, and so far there is always something showing up the wp-login.
Right now, my login works, but registering and reset password are not there yet. If you have that in stock you’ll make someone’s day today.
Cheers and long life to your site.
Hello Haider! There’s a misunderstanding! The snippets provided in the tutorial is not to perform logout action, it’ll just replace default logout link of WordPress with your existing logout page (if you have any). Thanks