1

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 ;
        }
  • 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 Oct 4 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 Oct 4 at 19:36
  • Have a look at my answer.. that might help. – Sally CJ Oct 4 at 19:41
  • Or did you mean, you've got a logout.php page on your site (example.com/logout.php)? If so, how exactly the hook doesn't work? (PS: In my previous comment, I meant to say "add_filter") – Sally CJ Oct 4 at 19:49
  • I still see wp-login.php in exit link – Tai Oct 4 at 19:50
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.com/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
  • 1
    add_filter('logout_url', 'my_custom_logout_url', 20, 2); that help me. Thanks! – Tai Oct 4 at 19:55
  • You're welcome.. I was actually about to ask you to change the callback priority.. and I updated my answer for completeness. :) Cheers! – Sally CJ Oct 4 at 20:07
0

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

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.