0

I have a Wordpress site with WooCommerce. I would like to add a Logout option to the Menu that logs out the user without asking for confirmation.

So I included a menu custom link with as URL: /my-account/customer-logout/?_wpnonce=3d7c353c19&redirect_to=http%3A%2F%2Fwww.example.com. However, the nonce changes every time, so it will still ask for confirmation to log out...

This post shares something about how to get rid of the confirmation request with php. But I'm not familiar with php. How should I use this in the URL field of the custom link? That is, how can I populate the custom url with the dynamic nonce?

7
  • Without PHP you probably can't do this. – kero Feb 16 at 14:33
  • If you use wp_logout_url() and pass it the urn (without the domain) that you want the user to be redirected to after logout, you will not see any confirmation request. – Buttered_Toast Feb 16 at 14:34
  • @Buttered_Toast, the problem is I have no idea how to use wp_logout_url() or pass it something... – Nick Feb 16 at 14:37
  • @Buttered_Toast I think OP is currently only using wp-admin tools, where they manually added a menu item with the posted URL. @Nick wp_logout_url() is a PHP method, you need to familiarize yourself with PHP, find a plugin that solves this, or hire someone to do it for you. – kero Feb 16 at 14:42
  • All right, thanks, I was afraid of that. It just felt strange that it's not a regular option in Wordpress itself. – Nick Feb 16 at 14:50
0

Using wp_logout_url() is your best choice.

You will need to create a <a> tag and in the href attribute output wp_logout_url()

<a href="<?= wp_logout_url('/'); ?>" title="Logout">Logout</a>

I passed / as an argument because after the user will click the link he will be redirected back to the homepage, you can change it to what ever you want

EDIT

Using code snippets you could create a shortcode and then use it, almost, where ever you want.

add_shortcode('bt_custom_logout_link', 'bt_custom_logout_link');
function bt_custom_logout_link ($atts) {
    $link = '<a href="' . wp_logout_url('/') . '" title="Logout">Logout</a>';
    return $link;
}

This function will register a new shortcode named bt_custom_logout_link, to use it you need to type it like this [bt_custom_logout_link]

7
  • 2
    I don't think Wordpress accepts that as a custom link. When I enter that and click on Save, Wordpress converts the url to http://a%20href=?=%20wp_logout_url('/');%20?%20title=LogoutLogout/a. See: ibb.co/yy66S1F – Nick Feb 16 at 15:14
  • Seems like you are trying to add this code inside a wysiwyg as link option, this is not possible. wysiwyg with link option will escape any special characters. you will need to go inside your theme and add it there. or find the right action to hook into it and output the code – Buttered_Toast Feb 16 at 15:18
  • I'm using the default Appearance > Menus option that comes with Wordpress. I've now also installed wordpress.org/plugins/code-snippets. Is it perhaps also possible with a code snippet? – Nick Feb 16 at 15:25
  • Edited my answer to include the code you need to add to code snippets in order to create a shortcode that will output a logout link – Buttered_Toast Feb 16 at 15:33
  • 2
    There's no wysiwyg editor available under Wordpress's Appearance > Menus. I don't think that is something available here in Wordpress. – Nick Feb 16 at 15:58
0

I ended up solving it using the plugin Code Snippets and the following code snippet based on https://wordpress.stackexchange.com/a/67342:

function change_menu($items){
    foreach($items as $item){
        if( $item->title == "Log Out"){
            $item->url = wp_logout_url('/');
        }
    }
    return $items;
}

add_filter('wp_nav_menu_objects', 'change_menu');

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.