最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Log out without confirmation request (nonce)

programmeradmin1浏览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. 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?

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. 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?

Share Improve this question asked Feb 16, 2021 at 14:28 NickNick 1272 silver badges9 bronze badges 7
  • Without PHP you probably can't do this. – kero Commented Feb 16, 2021 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 Commented Feb 16, 2021 at 14:34
  • @Buttered_Toast, the problem is I have no idea how to use wp_logout_url() or pass it something... – Nick Commented Feb 16, 2021 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 Commented Feb 16, 2021 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 Commented Feb 16, 2021 at 14:50
 |  Show 2 more comments

2 Answers 2

Reset to default 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]

I ended up solving it using the plugin Code Snippets and the following code snippet based on https://wordpress.stackexchange/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');
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>