Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
logout link

How to Add a WordPress Logout Link to a Navigation Menu

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The safest way to add a clickable logout control is to use WordPress’s wp_logout_url() function, escape the result with esc_url(), and place that URL in an anchor or state-aware menu output. If you do not edit theme code, the Login or Logout Menu Item plugin provides a menu workflow for both classic menus and the Navigation block.

Choose the approach that fits your site

Approach Best for Login/logout switching Menu types Redirect control Dependency
Theme code with WordPress functions Administrators who can safely edit a child theme or custom plugin Use wp_loginout() for automatic switching, or a direct logout anchor for a logout-only link Any location where your theme permits custom markup or a menu filter Pass a destination such as home_url() or the current permalink None beyond WordPress core
Login or Logout Menu Item plugin Code-free setup Yes Classic Appearance > Menus and the Navigation block Plugin settings provide login and logout redirect options Requires an additional plugin

The correct code insertion point depends on your theme. A hook documented for one theme is not automatically valid for another.

Option 1: Add a logout link with theme code

Use the core logout URL function

WordPress Developer Resources describes wp_logout_url() as returning “the URL that allows the user to log out of the site.” It was introduced in WordPress 2.7.0 and creates a URL with the logout action, a nonce, and an optional encoded redirect_to value. See the wp_logout_url() reference.

For a link that always appears as “Log out,” add this pattern to a theme template or another controlled PHP output location:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<a href="<?php echo esc_url( wp_logout_url( home_url() ) ); ?>">Log out</a>

home_url() makes the site home page the destination after logout. If you omit the argument, WordPress uses its default behavior. The function documentation also demonstrates using get_permalink() when you want to return the visitor to the current page.

Escape the generated URL

Use esc_url() when placing the generated value in an HTML href. WordPress’s own wp_loginout() implementation escapes the URL in the same way; see the wp_loginout() reference.

Make one menu control switch between Login and Logout

wp_loginout( $redirect, false ) returns link markup instead of echoing it. WordPress selects a Login link for logged-out visitors and a Logout link for authenticated visitors:

<?php echo wp_loginout( home_url(), false ); ?>

The second argument, false, is important: it requests a returned string so you can place the markup where your menu output is assembled. This function does not, by itself, insert an item into every WordPress menu. Your theme still needs the appropriate menu location, filter, or template markup.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Insert the output in the theme’s actual navigation location

Find the menu location your theme renders before choosing a hook. The WordPress reference includes a contributed example using wp_nav_menu_secondary_items for Twenty Fourteen’s secondary navigation. That hook is theme-specific, not a universal WordPress menu hook; do not copy it into another theme without confirming that the theme applies it.

For a block theme, navigation is normally edited through the Site Editor rather than by adding PHP to a classic menu template. For a classic theme, inspect the theme’s registered menu locations and rendering functions, then add the generated markup only where the theme expects valid menu-item HTML.

Option 2: Add it without code using Login or Logout Menu Item

The Login or Logout Menu Item plugin supplies a ready-made dynamic item and documents workflows for both classic menus and the Navigation block. Its labels and URL placeholders are plugin-specific; they are not native WordPress menu fields.

Classic menu workflow

  1. In the WordPress dashboard, go to Plugins and install Login or Logout Menu Item from the WordPress.org directory.
  2. Activate the plugin.
  3. Open Appearance > Menus and select the menu assigned to your site’s navigation location.
  4. Add the plugin’s Login|Logout item, arrange it where visitors should see it, and save the menu.
  5. Open the plugin’s settings if you need separate login and logout redirect destinations.

Navigation block workflow

  1. Open the Site Editor or the page/template that contains the Navigation block.
  2. Add a link inside the Navigation block.
  3. Search for “login” or “logout” as directed by the plugin’s instructions.
  4. Select the plugin-provided item, place it in the navigation, and save the template or navigation.

Choose a safe post-logout destination

A redirect supplied to wp_logout_url() is encoded in the URL’s redirect_to parameter. Use a destination on the intended site, such as home_url() or a known internal page. The function reference notes that redirecting to another host requires allowing that host with the allowed_redirect_hosts filter; cross-host redirects should therefore be treated as a deliberate multisite or integration requirement, not a default.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do not call wp_logout() to build a link

wp_logout() performs the logout immediately when executed. WordPress’s reference explicitly advises using wp_logout_url() for a button or link a visitor can click. Putting wp_logout(); directly in a template, widget, or page-load callback can log out the administrator as soon as that code runs. See the wp_logout() reference.

Test the menu in both authentication states

  • Open the site in a normal browser window while logged in and confirm the item says Logout (or Log out) and points to a URL containing the expected destination.
  • Sign out, then reload the navigation and confirm a state-aware item changes to Login.
  • Test in a private window or a separate browser session so an administrator cookie does not hide logged-out behavior.
  • Check keyboard focus, the visible label, and the final landing page after clicking the link.
  • Verify the item appears in the intended desktop and mobile menu locations; responsive menus can render a different navigation instance.

Troubleshoot stale labels and unexpected redirects

The label stays wrong after login or logout

The plugin listing warns that caching can serve the logged-in and logged-out versions of a menu incorrectly. Review page-cache, reverse-proxy, CDN, and optimization settings for authenticated traffic, then purge affected caches and retest. This is a troubleshooting lead rather than a guaranteed diagnosis.

The visitor lands on an unexpected page

Check the redirect value passed to wp_logout_url() or the plugin’s redirect settings. The plugin listing also notes that other plugins may override redirects, especially membership or login plugins. Temporarily inspect those plugins’ redirect rules before changing the menu code.

The item does not appear at all

Confirm that you edited the menu or Navigation block actually rendered by the active theme, that the menu is assigned to the visible location, and that a block theme’s template has been saved. For custom PHP, verify that the chosen hook is supported by that theme; the Twenty Fourteen secondary-navigation example is not portable by default.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended setup

For a code-capable site, use wp_loginout( home_url(), false ) when one navigation item should change between Login and Logout, or the escaped wp_logout_url() anchor when the menu is specifically for authenticated users. For a code-free site, install Login or Logout Menu Item and follow its classic-menu or Navigation-block workflow. In either case, use an internal post-logout destination, test both session states, and account for caching before treating the menu as finished.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.