Skip to:
Content
Pages
Categories
Search
Top
Bottom

Preventing bbPress access via redirection to WordPress login


  • jpmendoza
    Member

    @jpmendoza

    Hi guys,

    This is my first post here, but I’ve been building an wp/bb integrated site for a few weeks now, and I’ve come to the point where I think it’s time to write my first plugin. I want this plugin to redirect users to my WordPress login page if they are not logged in. It is important that the forums remain private and also that there is only one point of entry to the site for users, so as to avoid any confusion.

    I have _ck_’s Hidden Forums plugin set up to control access to forums, and that’s working fine, but I need some advice on how to handle an issue or two in the code I’ve written so far. Now, I don’t do a ton of programming, and this is my first attempt at anything like this, so bear with me if I’ve committed any cardinal sins.

    <?
    add_action('bb_init','login_redirect_init',300);
    function login_redirect_init()
    {
    global $bb_current_user;
    $id = (!empty($bb_current_user)) ? intval($bb_current_user->ID) : 0;
    if( $id < 1 )
    {
    $redirection = 'http://'.$_SERVER['SERVER_NAME'].'/caa/wp-login.php?redirect_to='.$_SERVER['REQUEST_URI'];
    if (function_exists('status_header'))
    status_header( 302 );
    header("HTTP/1.1 302 Temporary Redirect");
    header("Location:".$redirection);
    exit();
    }
    }
    ?>

    As it is now, accessing bbPress redirects to wp-login with the proper redirection URL (I’m using the WP Members Only plugin), but then logging in from there kicks me back to the wp-login page again. I’m thinking that perhaps bb_init is not the hook for me, as it appears that the login process is not getting a chance to do its thing, and thus I’m getting booted out. As a test, I tried bb_head, but of course the headers have been sent by then, and the redirect does not work.

    So, if this is in fact the issue, what hook should be using? Or am I going about this all wrong? I’d really appreciate any and all input into this. Thanks, guys.

Viewing 5 replies - 1 through 5 (of 5 total)

  • johnhiler
    Member

    @johnhiler

    I’m not expert enough to give advice on what hooks to use… but it might be worth checking out this related plugin (and ck’s recent comment about its general approach):

    https://bbpress.org/plugins/topic/force-login/

    Good luck!


    jpmendoza
    Member

    @jpmendoza

    Sweet. I’ll definitely look into this.

    Thanks!


    jpmendoza
    Member

    @jpmendoza

    I’ve tried this as well: if( !wp_validate_auth_cookie() )

    but haven’t had any luck.


    johnhiler
    Member

    @johnhiler

    Btw, do you need to have only one point of signin? I used to do that too (only use the bbPress signin, in my case)… but eventually I gave up and started using both. There has been no confusion or technical problems from users, and each signin does a good job with its side of the *Press fence!


    jpmendoza
    Member

    @jpmendoza

    It would be nice to have only one point of signin.

    If nothing else works, I guess I’ll just use Hidden Forums and a custom message or something.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar