简体   繁体   中英

localStorage.clear not working properly in Wp

I want to clear window.localStorage.clear after login and logout. I have added this code in the functions.php but it's not working. How can I overcome this?

add_action(' wp_logout ',' auto_redirect_external_after_logout ');
function auto_redirect_external_after_logout(){
    echo '<script>window.localStorage.clear();</script>';
    exit();
}

function do_anything() {
    echo '<script>window.localStorage.clear();</script>';
}
add_action('wp_login', 'do_anything');

I think echo in PHP will only output the string but it won't execute the JS which is inside the string. Hence your localStorage is not getting cleared. Otherwise the syntax you wrote for clearing localStorage is right.

A bug in the sample code (which may be the only reason it's not working as desired) is the whitespace in the two string literals that are IDs:

add_action(' wp_logout ',' auto_redirect_external_after_logout ');

Should be

add_action('wp_logout', 'auto_redirect_external_after_logout');

The whitespace on the event name means your hook is attached to an event nobody's sending, and I'd expect the extra spaces around the function name to result in "registered function not found".

(I'm reading./wp-includes/plugin.php and class-wp-hook.php, and it all flows to a call to php-builtin call_user_func() .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM