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
|
2 Answers
Reset to default 1There'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
$logout_url
part in your code) because the first parameter passed to your function is already the logout URL. Secondly, youradd_action()
call is missing the fourth parameter. – Sally CJ Commented Oct 4, 2019 at 19:33logout.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