I am making a Logout button, but in order for it to work properly the link needs to have the correct nonce ("site.com/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?


Use wp_logout_url()


<a href="<?php echo wp_logout_url(); ?>">Logout</a>
  • I have tried that but it just takes me litereally to link "website.com/<?php echo wp_logout_url(); ?>" ... – Filipe Rocha Sep 2 '20 at 10:00
  • Where are you putting the code? – drcrow Sep 2 '20 at 16:26
  • I am putting it in a HTML module through elementor – Filipe Rocha Sep 2 '20 at 19:29
  • This is PHP, will not work this way – drcrow Sep 2 '20 at 19:32
  • how can i make it work, please? – Filipe Rocha Sep 4 '20 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.