Skip to content

Everything for WordPress, web development — and beyond

🔐 Login and logout button in WordPress menu: a step by step guide 2026

🔐 Login and logout button in WordPress menu: a step by step guide 2026

Standard WordPress cannot display a "Login" button for logged-out visitors and a "Logout" button for logged-in users in the same menu location. At first glance this seems minor. But for a site with a member dashboard, membership area, or forum this is a basic need, and without it navigation turns into a guessing game.

Previously the BAW Login/Logout Menu plugin handled this task. The author discontinued the project in 2025, leaving thousands of sites without updates. Its direct successor has taken over: Login or Logout Menu Item with 20,000+ active installations and a 4.8 rating on WordPress.org.

We've compiled everything into one guide: from installing the plugin in two minutes to custom role-based redirects and disabling the admin bar for regular users.

💡 Quick overview:

  • Install the Login or Logout Menu Item plugin and configure three redirect fields
  • Add a "Login|Logout" menu item through the classic or block editor
  • Set up fine-tuned logic via functions.php snippets: role-based redirects, logout without confirmation, replacing wp-login.php
  • Hide the admin bar from regular users and block their access to wp-admin

Method 1: Login or Logout Menu Item plugin, fast and code-free

Login or Logout Menu Item plugin settings page in WordPress admin

The simplest way to add a dynamic login and logout button is the Login or Logout Menu Item plugin. It is the direct successor to BAW Login/Logout Menu, written from scratch by one of the original project's contributors. As of June 2026 the plugin has 20,000+ active installations, 23 reviews, and a 4.8-star rating. Version 1.3.2 has been tested up to WordPress 7.0.

Why this plugin instead of a code solution? The plugin does not touch functions.php and will not break when you switch themes. It supports the block editor, and the "Login|Logout" item can be added to a Navigation block through regular search. It also has a dedicated settings page: no magic URLs in menu fields, everything is transparent.

Installation and configuration

Install the plugin the standard way: Plugins → Add New, type Login or Logout Menu Item, click Install and Activate. After activation a new item appears in the admin menu: Settings → Login or Logout. There are three fields, and here is what they mean:

  • Login Page URL: the address of the login page. For most sites /wp-login.php is sufficient. If you use a custom login page (for example /login/ via MemberPress or WooCommerce), enter it here.
  • Login Redirect URL: where the user lands after logging in. Usually /account/ or simply / for the homepage. Note that membership plugins like MemberPress or Restrict Content Pro may override this redirect with their own settings.
  • Logout Redirect URL: where to send the user after logging out. Setting / or /wp-login.php makes sense.

Adding the menu item

Go to Appearance → Menus. In the upper right corner click Screen Options and verify that the Login/Logout checkbox is enabled. A "Login/Logout" block will appear in the left column; check Login|Logout and click Add to Menu.

The menu item will appear in the list. Its URL should remain #lolmiloginout# because this is the plugin's trigger; do not change it. You can edit the Navigation Label: for example "Login | Logout" or "My Account". Save the menu.

If you are working with the block editor (Appearance → Editor, Navigation block): click "+" to add a link, type "login" or "logout" in the search, and select "Login|Logout" from the suggestions. The link will be added automatically with the correct URL. You can also enter it manually: #lolmiloginout#.

What to do if the menu does not switch

The most common cause is caching. Disable cache for logged-in users in your caching plugin settings: WP Rocket, Cloudflare APO, LiteSpeed. The second cause is another plugin intercepting the redirect. Check your membership or security plugin settings: many of them have their own post-login redirect rules that take priority.

Method 2: login and logout via code in functions.php

The plugin covers most typical scenarios. But sometimes you need more granular logic: different redirects for different roles, forced logout without confirmation, hiding the admin bar. This is where functions.php comes in.

Add all the code below through the Code Snippets plugin; this is safer than editing functions.php directly. A syntax error will not crash the site; it will simply disable the snippet.

Login with role-based redirect

This code redirects administrators to the admin dashboard and everyone else to a member account page. Add it to Code Snippets or to your child theme's functions.php.

1/**
2 * Redirect after login: admin → wp-admin, others → /account
3 */
4function sd_redirect_by_role( $url, $request, $user ) {
5 if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
6 if ( $user->has_cap( 'administrator' ) ) {
7 $url = admin_url();
8 } else {
9 $url = home_url( '/account' );
10 }
11 }
12 return $url;
13}
14add_filter( 'login_redirect', 'sd_redirect_by_role', 10, 3 );

How it works: the login_redirect filter fires at the moment of login. The function checks the role via has_cap('administrator') and returns the appropriate URL. Priority 10 is standard, and 3 limits the number of accepted arguments. If your account page slug differs from /account, replace it in the home_url('/account') line.

Logout without confirmation

By default WordPress shows a confirmation page when you click a logout link: "Are you sure you want to log out?" On mobile devices this is especially annoying. Here is code that removes the confirmation:

1/**
2 * Logout without confirmation, redirect to home
3 */
4add_action( 'check_admin_referer', 'sd_logout_without_confirm', 10, 2 );
5function sd_logout_without_confirm( $action, $result ) {
6 if ( $action === 'log-out' && ! isset( $_GET['_wpnonce'] ) ) {
7 $redirect_to = isset( $_REQUEST['redirect_to'] )
8 ? $_REQUEST['redirect_to']
9 : home_url( '/' );
10 $location = str_replace( '&', '&', wp_logout_url( $redirect_to ) );
11 header( "Location: $location" );
12 die;
13 }
14}

Before adding this, make a full site backup. The function intercepts the nonce check during logout and performs the redirect immediately, bypassing the confirmation page. After installation test it: open your-domain/wp-login.php?action=logout in incognito mode; you should be redirected to the homepage without an intermediate screen.

Some security plugins (Wordfence, Solid Security) override logout behavior. If the confirmation still appears, disable their "confirm logout" option or add the URL /wp-login.php?action=logout to the whitelist.

Scenario: custom login page instead of wp-login.php

If you have replaced the standard login page with a custom one (for example through MemberPress or WooCommerce), you need to redirect direct access to wp-login.php:

1/**
2 * Replace wp-login.php with custom login page
3 */
4add_action( 'init', 'sd_custom_login_page' );
5function sd_custom_login_page() {
6 global $pagenow;
7 if ( ! is_user_logged_in() && 'wp-login.php' === $pagenow ) {
8 wp_redirect( home_url( '/login' ) );
9 exit;
10 }
11}

Replace /login with your login page slug. This is useful when you use a member dashboard plugin and do not want to expose the standard WordPress form.

How to hide the admin bar for everyone except administrators

The WordPress admin bar at the top of the site is useful only for those who manage the site. Regular users do not need it, and it clutters the interface, especially if you are building a member dashboard with a clean design.

Disabling the admin bar via code

Add this to Code Snippets or your child theme's functions.php:

1/**
2 * Hide admin bar for everyone except administrators
3 */
4add_action( 'after_setup_theme', 'sd_remove_admin_bar_for_non_admins' );
5function sd_remove_admin_bar_for_non_admins() {
6 if ( ! current_user_can( 'administrator' ) && ! is_admin() ) {
7 show_admin_bar( false );
8 }
9}

The function current_user_can('administrator') checks the role, and ! is_admin() ensures that the bar remains visible in the admin area itself, where anyone with access needs it.

Blocking access to wp-admin

Hiding the bar is not enough. You also need to block access to /wp-admin/ for those who do not need it:

1/**
2 * Block access to wp-admin for non-administrators
3 */
4add_action( 'admin_init', 'sd_block_wp_admin_for_non_admins' );
5function sd_block_wp_admin_for_non_admins() {
6 if ( ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
7 wp_redirect( home_url( '/restrict_user' ) );
8 exit;
9 }
10}

The DOING_AJAX condition is critical: without it you will block AJAX requests from member dashboard plugins that users need to execute from their profile. Replace the /restrict_user page with any placeholder page, for example the homepage or a page with an "Access denied" message.

⁉️🤔 Frequently asked questions

The Login or Logout Menu Item plugin does not work with PHP 8.0, what should I do?

There is one negative review on WordPress.org about incompatibility with PHP 8.0 from January 2026. However the plugin has been tested up to WP 7.0, and 20,000+ active installations confirm stable operation on modern PHP versions. If you encounter an error: verify the plugin version (should be 1.3.2), disable other redirect plugins, clear the cache. On a test site with PHP 8.2 no problems were found.

Can I do this without a plugin, using only code?

Yes. Method 2 in this article fully covers login, logout, and redirects via functions.php. The plugin is more convenient: settings from the admin panel without editing files, block editor support, automatic updates. If the site is simple and you only need a login and logout button, use the plugin. If the logic is complex (different redirects by role, multiple login pages), use code.

What should I do if the menu item does not appear in the block editor?

In the Navigation block click "+" and type "login" in the search (not "lolmiloginout"). If the item does not appear, verify that the plugin is active and your WordPress version is 6.0 or higher. For older themes without the block editor use the classic interface at Appearance → Menus.

How do I set up redirects for different roles through the plugin?

Login or Logout Menu Item does not distinguish between roles; it performs one redirect for all users. For role-based redirects use the code from Method 2: the sd_redirect_by_role function works even if the menu item was added via the plugin. The login_redirect filter executes after the plugin's redirect and overrides it.

Why does the logout confirmation page still appear?

Some security plugins (Wordfence, Solid Security) override logout behavior at their own level. Disable their logout confirmation option or add the logout URL to the whitelist. The code from Method 2 fires before most plugins, but not all; in rare cases you may need to configure the security plugin itself.

How does WP-Recall differ from Login or Logout Menu Item?

WP-Recall is a full-featured member dashboard plugin: profiles, private messages, groups, commerce modules. Login and logout are built in as part of the ecosystem. Login or Logout Menu Item solves exactly one task: a dynamic login/logout button in the menu. If you need a complete member dashboard with many features, look at WP-Recall or MemberPress. If you only need a button in the menu, Login or Logout Menu Item is lighter and does not bring extra baggage.

What to use on your site: plugin or code

The quick solution for a typical site is the Login or Logout Menu Item plugin. Install it, fill in three fields in Settings, add the menu item. Everything works in five minutes. Twenty thousand active installations and compatibility with WP 7.0 confirm that the tool is mature and maintained.

If your login logic is non-trivial (different redirects for different roles, integration with a membership plugin, custom login page), use the code from Method 2. It is more flexible but requires caution. Write code through Code Snippets, not directly in the theme file. One typo in functions.php and the site goes down.

A combination of the plugin for the menu and code for redirects is the compromise we use on most projects. The plugin provides convenient menu item management and block editor support. Custom code handles specific role and redirect requirements without complicating the plugin's settings.

Choose based on your task, not someone else's checklist. For a simple site with a membership area the plugin is enough. For a marketplace or educational platform, code is essential. Either way, keep a backup before making changes and test on a staging copy.