Hook to change Logout url

I used this hook to replace the exit address .Now, unfortunately, this hook does not work and I can not understand whya

I used this hook to replace the exit address . Now, unfortunately, this hook does not work and I can not understand why

add_filter('logout_url', 'my_custom_logout_url');
        function my_custom_logout_url($force_reauth, $redirect=null){
        $logout_url = wp_nonce_url(site_url('logout.php')."?action=logout", 'log-out' );
        if (empty($redirect)) $redirect=home_url();
        $logout_url = add_query_arg('redirect_to', urlencode( $redirect )."", $logout_url );
        return $logout_url ;
        }

I used this hook to replace the exit address . Now, unfortunately, this hook does not work and I can not understand why

add_filter('logout_url', 'my_custom_logout_url');
        function my_custom_logout_url($force_reauth, $redirect=null){
        $logout_url = wp_nonce_url(site_url('logout.php')."?action=logout", 'log-out' );
        if (empty($redirect)) $redirect=home_url();
        $logout_url = add_query_arg('redirect_to', urlencode( $redirect )."", $logout_url );
        return $logout_url ;
        }
Share Improve this question asked Oct 4, 2019 at 18:23 TaiTai 133 bronze badges 5
  • No need to regenerate the logout URL (the $logout_url part in your code) because the first parameter passed to your function is already the logout URL. Secondly, your add_action() call is missing the fourth parameter. – Sally CJ Commented Oct 4, 2019 at 19:33
  • Previously, the hook worked correctly. After some update it stopped working. The hook replaced the links for users with wp-login.php/..... by logout.php/..... – Tai Commented Oct 4, 2019 at 19:36
  • Have a look at my answer.. that might help. – Sally CJ Commented Oct 4, 2019 at 19:41
  • Or did you mean, you've got a logout.php page on your site (example/logout.php)? If so, how exactly the hook doesn't work? (PS: In my previous comment, I meant to say "add_filter") – Sally CJ Commented Oct 4, 2019 at 19:49
  • I still see wp-login.php in exit link – Tai Commented Oct 4, 2019 at 19:50
Add a comment  | 

2 Answers 2

Reset to default 1

There's no need to regenerate the logout URL (the $logout_url part in your code) because the first parameter passed to your function is already the logout URL.

So basically, just rename that $force_reauth to $logout_url and remove the $logout_url = wp_nonce_url( ... );:

function my_custom_logout_url($logout_url, $redirect=null){ // rename the $force_reauth
    // And remove this:
    //$logout_url = wp_nonce_url(site_url('logout.php')."?action=logout", 'log-out' );

Secondly, your add_filter() call is missing the fourth parameter (which is the number of arguments passed to your function):

add_filter('logout_url', 'my_custom_logout_url', 10, 2); // like this
add_filter('logout_url', 'my_custom_logout_url');        // not this

UPDATE

If you actually did mean to set the logout URL to example/logout.php (note the logout.php), then your code is actually good (except the add_filter() thing above). But if your code is still showing wp-login.php, then it's possible another code (maybe a plugin) is filtering/changing the logout URL. And in that case, you can change the callback priority to a greater number like 20:

// The third parameter is the callback priority.
add_filter('logout_url', 'my_custom_logout_url', 20, 2); // the priority is now 20

I created a page named "log-out"

And then, in page-log-out.php I added this (the cookies lines can be avoided)

wp_logout();
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie("user_role", '', time()-1000, '/', $domain, false, true);
setcookie("user_id", '', time()-1000, '/', $domain, false, true);
setcookie("full_name", '', time()-1000, '/', $domain, false, true);
wp_redirect(home_url());

Hope it works for you

I find this too, check there

How to change the default logout link on WordPress Admin

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745105200a4611514.html

相关推荐

  • Hook to change Logout url

    I used this hook to replace the exit address .Now, unfortunately, this hook does not work and I can not understand whya

    12小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信