admin管理员组

文章数量:1323677

I am making a Logout button, but in order for it to work properly the link needs to have the correct nonce ("site/wp-login.php?action=logout&wp_nonce=X". I have found in another question (How to log out without confirmation 'Do you really want to log out?"?) a way to add the nonce, but it is only for menu links:

  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');

How can I do the same for a button?

I am making a Logout button, but in order for it to work properly the link needs to have the correct nonce ("site/wp-login.php?action=logout&wp_nonce=X". I have found in another question (How to log out without confirmation 'Do you really want to log out?"?) a way to add the nonce, but it is only for menu links:

  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');

How can I do the same for a button?

Share Improve this question asked Sep 1, 2020 at 18:05 Filipe RochaFilipe Rocha 1011 bronze badge 1
  • I have found the best solution and explanation: scratchcode.io/how-to-logout-without-confirmation-in-wordpress – Mayank Dudakiya Commented Dec 20, 2020 at 7:25
Add a comment  | 

1 Answer 1

Reset to default 0

Use wp_logout_url()

https://developer.wordpress/reference/functions/wp_logout_url/

<a href="<?php echo wp_logout_url(); ?>">Logout</a>

本文标签: phpChange button link to add nonce