0

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?

1

1 Answer 1

0

Use wp_logout_url()

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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