dongzuo7166 2018-08-14 14:31
浏览 106

Wordpress plugindevelopment:is_user_logged_in返回false

I'm developing a plugin that'll manage a user log in. In my custom log in form, when credentials are submitted I run this code:

if( isset( $_POST['login'] ) ){

    $login = wp_signon( array(
        'user_login' => $_POST['username'],
        'user_password' => $_POST['password'],
        'remember' => 'true'
    ), true);

    if( is_wp_error( $login ) ){
        echo $login->get_error_message();
    } else {

        $username = $_POST['username'];

        wp_set_current_user( $login->ID, $_POST['username'] );
        wp_set_auth_cookie( $login->ID );
        do_action( 'wp_login', $username );
    }//else

}//if


if( is_user_logged_in() ){ // is_user_logged_in()
    ?><a id="wp-submit" href="<?php echo esc_url( add_query_arg( array( 'ecofriends_logout' => true) ) ); ?>" title="Logout">Logout</a><?php


} else { 
    ecofriends_login_form(); 
}//else

So when the credential are submitted it logs in. If credentials aren't submitted check if the user is logged in to show a logout link else the login form.

The important fact is that here is_user_logged_in() works fine.

In the function.php file I created a function that manage the logout:

function logout(){
if( is_user_logged_in() ){
    wp_logout();
}//if
}//ecofriends_logout

but when I call it is_user_logged_in() always return false even if the user has just logged in.

P.S. I'm working on localhost and the login form is called in a page using a shortcode.

Thank you

  • 写回答

1条回答 默认 最新

  • doubi8383 2018-08-16 14:15
    关注

    Ok, from the comments I think this is what's happening. Your plugin login.php file is invoked on every request, which you said calls the logout() function, ergo logging user out every time.

    You need to invoke your logout() function on user triggered action, like clicking on a log out button or in certain conditions you impose.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)