How to Add the WordPress Logout Link to Navigation Menu
Adding a logout option directly inside your site’s navigation makes it much easier for logged-in users to sign out from any page. This is especially important on membership sites, client portals, learning platforms, and any WordPress site where users regularly log in to access protected content.
Table of contents
Why You Should Add a Logout Link to Your Menu
By default, WordPress does not add a logout button to your primary navigation. Instead, users must log out via the admin toolbar or a dedicated page. That is not always intuitive, and many non-technical users struggle to find how to sign out.
Placing a logout option in your menu has several advantages:
- Better user experience: Users expect to see login and logout in a predictable location, usually the header or main navigation.
- Improved security: Clear sign-out options encourage users to log out of shared or public devices.
- Consistent design: You can align login/logout actions with the rest of your site navigation.
- Role-based navigation: You can show different menu items for logged-in and logged-out visitors.
There are several ways to integrate a WordPress logout link into your navigation menu, depending on your technical comfort level and theme setup. The following sections cover the most commonly used, reliable approaches.
Understanding the WordPress Logout URL
Before adding anything to your menu, you need the correct URL that triggers the logout action. WordPress provides a core function for this: wp_logout_url(). In practical usage, that function generates a special URL that:
- Logs the current user out, and
- Optionally redirects them to a specific page afterward.
If you are not writing PHP and just want a direct link, you can use a standard logout URL with a redirect parameter. The pattern is:
https://example.com/wp-login.php?action=logout&redirect_to=https%3A%2F%2Fexample.com%2F
Replace example.com with your own domain. The redirect_to parameter tells WordPress where to send the user after logging out — typically your homepage or a custom “You have been logged out” page.
With this in mind, let’s look at how to add the logout link directly into your navigation menu using the built-in menu editor, code snippets, and conditional logic.
Method 1: Add a Logout Link to the Menu Using the Built-In Menu Editor
This approach requires no coding and works with any theme that uses the WordPress menus system. You will be manually adding a custom link to your navigation and pointing it to the logout URL.
Step 1: Open the Menu Editor
In the WordPress admin area:
- Go to Appearance > Menus.
- Select the menu where you want the logout link to appear (usually the primary menu).
If your theme uses the block-based Site Editor (Full Site Editing), you may need to access your navigation from Appearance > Editor and then adjust the navigation block instead. The logic remains the same: you will add a custom link to your navigation structure.
Step 2: Create a Custom Link
Inside the classic Menus screen:
- Click Custom Links in the left-hand panel.
- In the URL field, paste your logout URL. For example:
https://your-site.com/wp-login.php?action=logout&redirect_to=https%3A%2F%2Fyour-site.com%2F - In the Link Text field, type something like Log Out or Sign Out.
- Click Add to Menu.
Drag the new menu item to the desired position in your navigation, then click Save Menu.
Step 3: Test the Logout Link
Visit your site as a logged-in user and click the new navigation item. You should be logged out and redirected to the URL defined in redirect_to.
This method is fast and does not involve editing code. However, the logout link displays for everyone, including logged-out visitors. To improve this behavior and only show it to logged-in users, you will need some conditional logic.
Method 2: Show the Logout Link Only for Logged-In Users
Ideally, your navigation should display a login link to logged-out visitors and a logout link to logged-in users. There are two general ways to achieve this:
- Using a plugin that adds visibility conditions to menu items.
- Using custom PHP to filter menu items based on login state.
Option A: Use a Menu Visibility Plugin
Several plugins extend the native navigation system with conditional display rules, allowing you to control exactly who sees each item. While plugin recommendations change over time, you can search the plugin directory for features such as:
- “Conditional menu items”
- “Menu visibility”
- “Show menu items for logged-in users only”
The general workflow with such a plugin is:
- Install and activate the plugin.
- Go to Appearance > Menus.
- Edit the Log Out menu item.
- Use the new visibility controls to set it to display only for logged-in users.
- Create a separate Log In (or “My Account”) menu item and set it to display only for logged-out users.
This approach keeps everything inside the UI and is friendly for non-developers, while still providing a dynamic login/logout experience in your navigation.
Option B: Use PHP to Control Menu Items
If you prefer to avoid additional plugins, you can use the wp_nav_menu_items or wp_get_nav_menu_items filters to modify menu items based on the current user’s login status.
Here is a simplified example using a filter that appends a logout link to a specific menu when a user is logged in. Add this to your child theme’s functions.php file or a dedicated functionality plugin:
<?php
add_filter( 'wp_nav_menu_items', 'mytheme_add_logout_link_to_menu', 10, 2 );
function mytheme_add_logout_link_to_menu( $items, $args ) {
// Replace 'primary' with the theme location of the menu you want to target
if ( 'primary' === $args->theme_location ) {
if ( is_user_logged_in() ) {
$logout_url = wp_logout_url( home_url() );
$items .= '<li><a href="' . esc_url( $logout_url ) . '">' . esc_html__( 'Log Out', 'textdomain' ) . '</a></li>';
} else {
$login_url = wp_login_url( home_url() );
$items .= '<li><a href="' . esc_url( $login_url ) . '">' . esc_html__( 'Log In', 'textdomain' ) . '</a></li>';
}
}
return $items;
}
This snippet does the following:
- Targets the
primarymenu (you can adjust this to match your theme). - Checks
is_user_logged_in(). - Appends a Log Out link for logged-in users that redirects to the homepage.
- Appends a Log In link for logged-out visitors.
With this method, you do not need to manually add a logout item in the Menus screen. The items are generated automatically based on the current user state.
Method 3: Add a Dedicated Logout Page and Link It in the Menu
Another clean pattern is to create a dedicated page that logs the user out as soon as they visit it, then redirects them somewhere else. This gives you more control over the user journey and allows you to add custom messages or branding around the logout action.
Step 1: Create a Logout Page
In the WordPress admin dashboard:
- Go to Pages > Add New.
- Give the page a clear title, such as Log Out.
- In the content area, you can leave it empty or include a short message such as “Logging you out…” that will briefly appear.
- Publish the page.
Step 2: Add a Redirect Template or Shortcode
There are two main ways to make this page automatically log the user out:
- Use a page template in your theme or child theme that calls
wp_logout_url()and redirects. - Use a shortcode that performs the logout action when the page is loaded.
Here is an example using a shortcode. Add this to your functions.php or functionality plugin:
<?php
function mytheme_auto_logout() {
if ( is_user_logged_in() ) {
// Redirect to home page after logout
$redirect_url = home_url( '/' );
wp_safe_redirect( wp_logout_url( $redirect_url ) );
exit;
}
return '<p>' . esc_html__( 'You are already logged out.', 'textdomain' ) . '</p>';
}
add_shortcode( 'auto_logout', 'mytheme_auto_logout' );
Then, edit your logout page in the Gutenberg editor and add a Shortcode block with:
[auto_logout]
When logged-in users visit this page, they will be logged out and redirected to your chosen URL. Logged-out users will simply see the fallback message.
Step 3: Link the Page in Your Menu
Now, go back to Appearance > Menus and add the new page to your navigation:
- In the left panel, select Pages.
- Check your Log Out page and click Add to Menu.
- Save your menu.
To avoid confusing logged-out visitors, combine this with conditional visibility (plugin-based or code-based) so the page only appears for logged-in users.
Handling Menu Behavior in Block Themes (Site Editor)
If you are using a block theme with the Site Editor, your navigation is managed as a block instead of through the classic Menus screen. You still have access to all the URLs and strategies above, but the workflow looks slightly different.
To add a logout item in a block-based navigation:
- Go to Appearance > Editor.
- Open the template or template part that contains your navigation (often called Header).
- Select the Navigation block.
- Use the block options to add a new Link item.
- Paste your logout URL or link to the dedicated logout page you created.
- Save your changes.
Conditional display of menu items in the Site Editor typically relies either on block-based conditional logic plugins or custom PHP filters affecting wp_nav_menu() output for block-rendered navigation. The underlying principles remain the same: show logout options to authenticated users and login or registration options to others.
Security and UX Best Practices for the Logout Link
When integrating a logout option in your main navigation, keep these best practices in mind:
- Always use proper URLs: Rely on
wp_logout_url()or the standard logout endpoint with aredirect_toparameter. Avoid hard-coding exotic or custom scripts unless you know exactly what they do. - Use safe redirects: If you build custom logout flows, use
wp_safe_redirect()to avoid open redirect vulnerabilities. - Keep labels clear: Use language your audience understands, such as “Log Out,” “Sign Out,” or “Exit Account.”
- Pair with a login link: Provide an obvious way for visitors to log in again, especially on membership or ecommerce sites.
- Consider mobile usability: Ensure the logout option is easy to tap on smaller screens and not placed near destructive actions like “Delete Account.”
Common Issues and How to Troubleshoot Them
Adding a WordPress logout link to your navigation is straightforward, but some edge cases can cause confusion. Here are frequent issues and quick ways to address them:
Logout Link Does Nothing or Refreshes the Page
If clicking the link appears to do nothing:
- Check that the URL is correct and includes
action=logout. - Ensure you are not caching logged-in pages aggressively. Logged-in users should typically be bypassed from full-page caching.
- Temporarily disable security or redirect plugins to see if they are intercepting the logout URL.
Wrong Redirect After Logging Out
If users are being redirected somewhere unexpected:
- Check the
redirect_toparameter in your URL or yourwp_logout_url()call. - Review membership or security plugins that might enforce their own logout redirections.
- Clear any server-level caching or custom rewrite rules that might alter query parameters.
Logout Link Visible to Logged-Out Visitors
If you do not use any conditional logic, this behavior is expected. To improve it:
- Use a menu visibility plugin to hide the logout item from logged-out visitors.
- Or implement
is_user_logged_in()-based filters to create separate menus for authenticated and unauthenticated users.
Conclusion
Integrating a logout link into your WordPress navigation is a small change that has a big impact on usability and security. Whether you use a simple custom link, conditional visibility rules, or a fully customized logout page, giving users a clear way to sign out is a hallmark of a well-designed user experience.
Start with the approach that best matches your technical comfort level: add a basic custom link, enhance it with visibility conditions, and later explore code-based solutions for more advanced, role-aware navigation. Whichever method you choose, your users will appreciate the clarity and convenience of having a prominent, reliable logout option in your main menu.