WordPress - Check if user is logged in

后端 未结 6 1607
逝去的感伤
逝去的感伤 2020-12-08 19:35

I am fairly new to WordPress. On my homepage I have a navigation bar which I only want to show to people who are logged in as users.

In my header.php th

6条回答
  •  抹茶落季
    2020-12-08 20:15

    I think that. When guest is launching page, but Admin is not logged in we don`t show something, for example the Chat.

    add_action('init', 'chat_status');
    
    function chat_status(){
    
        if( get_option('admin_logged') === 1) { echo "";}
            else { echo "";}
    
    }
    
    
    
    add_action('wp_login', function(){
    
        if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 1);
    });
    
    
    add_action('wp_logout', function(){
        if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 0);
    });
    

提交回复
热议问题