简体   繁体   English

创建一个注销WordPress用户的功能?

[英]Creating a function to logout a WordPress user?

I'm trying to create a function to logout WordPress users after a period of inactivity. 我正在尝试创建一个在一段时间不活动后注销WordPress用户的函数。 The timed element is working as it should and redirecting users after a set period of time. 定时元素正在按预期方式工作,并在设置的时间段后重定向用户。

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 问题在于,一旦加载了PHP脚本(在代码示例中),我便遇到以下错误: 致命错误:调用/ var / www / html / wp-content / plugins / ion-wp中未定义的函数wp_logout()第5行的-login-timeout / scripts / timed-logout.php

All the reference material I read tells me that wp_logout() should log out the user but instead there are errors. 我阅读的所有参考资料都告诉我wp_logout()应该注销用户,但存在错误。 I have tried several methods, including adding an action. 我尝试了几种方法,包括添加操作。 I do not want to redirect the user to a login screen. 我不想将用户重定向到登录屏幕。

Sample of code is below. 代码示例如下。 The echo statements are in place purely for testing. echo语句仅用于测试。 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 );

?>

You can make your function inside functions.php 您可以在functions.php中创建函数

add_action( 'wp_logout', 'redirect_after_logout');
function redirect_after_logout(){
  wp_redirect( 'http://example.com' );
  exit();
}

Then put this in your desire position of your site: 然后将其放在您网站的理想位置:

<a href="<?php echo wp_logout_url(); ?>" >Logout</a>

This must use in init action. 这必须使用init动作。
You can use wp_logout() functions that logout current user. 您可以使用wp_logout()函数注销当前用户。
the details of this function in wp-includes/pluggable.php file: wp-includes / pluggable.php文件中此函数的详细信息:

<?php
/**
 * Log the current user out.
 *
 * @since 2.5.0
 */
function wp_logout() {
    wp_destroy_current_session();
    wp_clear_auth_cookie();

    /**
     * Fires after a user is logged-out.
     *
     * @since 1.5.0
     */
    do_action( 'wp_logout' );
}

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. 从注销用户之前的过程的最后阶段开始,我将wpsessionexpired = true值发布回用户所在的页面。 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');

Logout function according user role (this is a working code) copy and paste in function.php file 根据用户角色注销功能(这是一个工作代码),将其复制并粘贴到function.php文件中

    function redirect_after_logout() {

        $current_user   = wp_get_current_user();
        $role_name      = $current_user->roles[0];

        if($role_name == 'subscriber'){
            $redirect_url = site_url();
            wp_safe_redirect( $redirect_url );
            exit;
        } 

    }
    add_action( 'wp_logout', 'redirect_after_logout'  );

this is also working for 这也为

      <?php 
if (current_user_can('client') OR current_user_can('salesrep') ){
?>
<a href="<?php echo wp_logout_url( home_url() ); ?>" class="btn btn-primary"
style="float: right;" >Logout</a>
<?php }
?>

you can set any role as your need. 您可以根据需要设置任何角色。 like is this example (client, salesrep) 就像这个例子(客户,salesrep)

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

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