Forums

Join
bbPress Support ForumsPluginsCheck if user is logged in in 1.0.1

Info

Check if user is logged in in 1.0.1

  1. I want to check if the visitor is logged in, I can't find a function that works in version 1.0.1 (such as bb_is_user_logged_in(), is_user_logged_in(), bb_is_logged_in() and is_logged_in() ). All of those functions raise the "Call to undefined function" error.

    I am trying to make my forum private, redirecting all users that are not logged in to the login page. The following code is in my functions.php theme file:


    if(!preg_match("/bb-login.php/", $_SERVER["SCRIPT_NAME"]) && !bb_is_user_logged_in()) {
    header("Location: " . $bb->uri . "bb-login.php");
    die("Authorisation required.");
    }

    Any help is appreciated.

  2. bb_is_user_logged_in() is right there, still in 1.0.1

    The problem is you are checking too early, right when the plugin loads which is too soon.

    A user is not determined as logged in until bb_init happens. So you have to make a mini-plugin

    add_action('bb_init','check_login');
    function check_login() {
    if(!preg_match("/bb-login.php/", $_SERVER["SCRIPT_NAME"]) && !bb_is_user_logged_in()) {
    header("Location: " . $bb->uri . "bb-login.php");
    die("Authorisation required.");
    }
    }
  3. Brilliant!

    Works perfectly when $bb->uri is replaced with bb_get_option('uri') .

    Many thanks for your fast reply.

  4. This topic is closed