Hi,
I want to add a logout link in the menu item.
If I add [wpv-logout-link] in the URL field, it doesn't work. And if I add /wp-login.php?action=logout&redirect_to=/my-account/ in the URL field, it does work, but it asks for the confirmation.
Is there anyway to create a logout link that logs out the user directly, without asking for their confirmation.
Thanks.
Hello and thank you for contacting the Toolset support.
By default, WordPress will not evaluate shortcodes inside the menus. Toolset does not actually have any effects on menus. However, you can run a shortcode inside a menu using a 3rd party plugin such as https://wordpress.org/plugins/shortcode-in-menus/
You may also want to use a specialize plugin such as https://wordpress.org/plugins/login-logout-menu/
I hope this helps. Let me know if you have any questions.
Hi Jamal,
For some reasons, the plugin didn't work for me. I did some research and came across a solution over here versteckter Link
/**
* Generates custom logout URL
*/
function getLogoutUrl($redirectUrl = ''){
if(!$redirectUrl) $redirectUrl = site_url();
$return = str_replace("&", '&', wp_logout_url($redirectUrl));
return $return;
}
/**
* Bypass logout confirmation on nonce verification failure
*/
function logout_without_confirmation($action, $result){
if(!$result && ($action == 'log-out')){
wp_safe_redirect(getLogoutUrl());
exit();
}
}
add_action( 'check_admin_referer', 'logout_without_confirmation', 1, 2);
I tried it out and it works.
First, if WP can't find a valid wpnounce parameter & value in the logout URL, it will display the logout confirmation page. The above code adds the necessary wpnounce parameter & value on the fly.
The next part is adding the logout URL in the menu item or wherever. The logout URL should be:
"/wp-login.php?action=logout"
That's it.
This code can be added in theme's function.php file or in a plugin like Code Snippets.