Add Logout with Nonce to wp_nav_menu()

I’ve seen a lot of suggestions online to copy the Logout link shown in the WordPress backend into a Custom Link when using wp_nav_menu().

However, this will redirect users to the Logout Confirmation screen, and they will be prompted to logout once more.

Use the below snippet in functions.php to target a Menu Item by title and slug to create a logout link with nonce:

function change_menu($items){
  foreach($items as $item){
    if( $item->title == "Logout"){ // Update to link title
         $item->url = $item->url . "&_wpnonce=" . wp_create_nonce( 'log-out' ); // do not change
  return $items;

add_filter('wp_nav_menu_objects', 'change_menu');
logout with nonce url


Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.