简体   繁体   English

localStorage.clear 在 Wp 中无法正常工作

[英]localStorage.clear not working properly in Wp

I want to clear window.localStorage.clear after login and logout.我想在登录和注销后清除window.localStorage.clear I have added this code in the functions.php but it's not working.我在functions.php中添加了这段代码,但它不起作用。 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. 我认为PHP中的echo将仅输出字符串,但不会执行字符串内部的JS。 Hence your localStorage is not getting cleared. 因此,您的localStorage不会被清除。 Otherwise the syntax you wrote for clearing localStorage is right. 否则,您编写用于清除localStorage的语法是正确的。

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:示例代码中的一个错误(这可能是它无法按预期工作的唯一原因)是两个作为 ID 的字符串文字中的空格:

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".事件名称上的空格表示您的挂钩附加到没人发送的事件,我希望 function 名称周围的额外空格会导致“未找到已注册的 function”。

(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() .) (我正在阅读。/wp-includes/plugin.php 和 class-wp-hook.php,这一切都流向了对 php-builtin call_user_func()的调用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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