WordPress Reference
< All Topics
Print

How to create a WordPress menu logout link that bypasses the “are you sure you want to log out” message

This is what worked for me.  First I created a custom log out menu link like the one below.  I named the link label “logout”

https://yoursite.com/wp-login.php?action=logout&redirect_to=https://yoursite.com

Then

I added the code below to my child theme’s functions.php file:

function change_menu($items){
  foreach($items as $item){
    if( $item->title == "logout"){
         $item->url = $item->url . "&_wpnonce=" . wp_create_nonce( 'log-out' );
    }
  }
  return $items;

}
add_filter('wp_nav_menu_objects', 'change_menu');

The above code adds the variable necessary to disable the message to the link labeled “logout”

Source: https://wordpress.stackexchange.com/questions/274257/using-nonce-in-menu-item

Table of Contents