创建一个功能来注销 WordPress 用户? [英] Creating a function to logout a WordPress user?
问题描述
我正在尝试创建一个功能,以便在一段时间不活动后注销 WordPress 用户.定时元素正常工作,并在一段时间后重定向用户.
I'm trying to create a function to logout WordPress users after a period of inactivity. The timed element is working as it should and redirecting users after a set period of time.
问题是,一旦加载了 PHP 脚本(在代码示例中),我就会遇到以下错误:致命错误:调用/var/www/html/wp-content 中的未定义函数 wp_logout()/plugins/ion-wp-login-timeout/scripts/timed-logout.php 第 5 行
The problem is that once the PHP script is loaded (in code sample), I run into the following error: Fatal error: Call to undefined function wp_logout() in /var/www/html/wp-content/plugins/ion-wp-login-timeout/scripts/timed-logout.php on line 5
我阅读的所有参考资料都告诉我 wp_logout() 应该注销用户,但出现错误.我尝试了几种方法,包括添加一个动作.我不想将用户重定向到登录屏幕.
All the reference material I read tells me that wp_logout() should log out the user but instead there are errors. I have tried several methods, including adding an action. I do not want to redirect the user to a login screen.
代码示例如下.echo 语句仅用于测试.任何帮助将非常感激.
Sample of code is below. The echo statements are in place purely for testing. Any help would be much appreciated.
<?php
function logout_this_session() {
//Logout Now
wp_logout();
wp_die();
}
echo 'This will be the logout script<br/><br/>';
$last_page = $_SERVER['HTTP_REFERER'];
echo 'You came from: ' . $last_page;
logout_this_session();
header( 'Location: ' . $last_page );
?>
推荐答案
我终于找到了一个解决方案,如下面的代码示例所示.从注销用户之前的最后一个阶段开始,我有一个 wpsessionexpired=true 值回发到用户所在的页面.用户注销后立即刷新同一页面.我把它放在主插件文件中.
I finally figured out a solution as in code sample below. From the last stage of the process before logging out the user, I have a wpsessionexpired=true value posted back to the page the user was on. The same page is immediately refreshed after the user is logged out. I placed this within the main plugin file.
function logoutUser(){
if ( $_POST["wpsessionexpired"] == 'true' ){
wp_logout();
header("refresh:0.5;url=".$_SERVER['REQUEST_URI']."");
}
}
add_action('init', 'logoutUser');
这篇关于创建一个功能来注销 WordPress 用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!