Support

Account

Home Forums General Issues Show online offline status automatically Reply To: Show online offline status automatically

  • You have several issues and I’m not sure where to start.

    This is the current user and would have nothing some other user that may or may not be logged in.

    
    is_user_logged_in()
    

    In order to update the field for the user when they log in you would need to use the wp_login hook and to set the value when they log out you would need to use the wp_logout hook.

    Unfortunately wp_logout only fires when the user actually logs out. A user can simply close their browser, this could log them out and make them unavailable without actually firing the logout hook.

    When getting or updating a field on a user you must supply the correct user ID to ACF. This would not be the value returned by is_user_logged_in(), this only returns the current user an could not be used to check a value on another user. So you need to determine what user you actually want to see if they are online and then construct the correct ID to give ACF.

    The post ID that ACF is expecting is

    
    $post_id = "user_{$user_id}";
    

    Then you can get or update the value for the field for that user

    
    // get field
    $online_status = get_field('onlinestatus', $post_id);
    // update field
    update_field('onlinestatus', $value, $post_id);
    

    When updating a field you should really used the field key and not the field name so the above should be something like this.

    
    // where field_XXXXXXX is the field key of the field named 'onlinestatus'
    update_field('field_XXXXXXX', $value, $post_id);
    

    In addition to this, when updating a group field ACF expects and array of field key => value pairs to be updated and you must supply a value for every sub field even if you are not changing the value. Any sub field the you do not include will likely be set to NULL.

    
    $value = array(
      'field_YYYYYYY' => 'value for field',
      'field_ZZZZZZZ' => 'value for field',
      // etc
    );
    update_field('field_XXXXXXX', $value, $post_id);
    

    Now what you need to figure out is how to get the Advisor user ID that is associated with the user that is currently logged in.