Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Redirect users to Registration other wise set Activity as front page

  • @braun_trutta

    Participant

    I’ve tried the following to redirect users to Registration page if not logged in and to the Activity page if logged in.

    In WordPress, I set the “Front page:” to Activity (Activity is a WordPress Page that is set to Activity Streams in the BP Page settings tab)

    In my theme’s functions.php file, I’ve added:

    add_action( 'init', 'redirect_visitors' );
    function redirect_visitors() {
        if ( !is_user_logged_in() && !bp_is_register_page()) {
    		wp_redirect( 'http://www.mysite.org/my-site-registration' );
            exit;
       }
    }

    However, when wp_redirect( 'http://www.mysite.org/my-site-registration' ) is run. There is a redirect loop on the registration page.

    I was led to doing it this way because if I set the WordPress Front page: to the page I have set as the registration page, the site redirects the visitor to the Members page once logged in. I haven’t found a way to change this to be anything other than the Members page.

    IN SUMMARY: I would like a user who visits the homepage of my site to see the Registration Page. I would like uses, once they are logged in, or already logged in, to see the Activity Stream as the front page.

Viewing 4 replies - 1 through 4 (of 4 total)
  • @tanner-m

    Participant

    Hey @braun_trutta! Try changing your action to ‘template_redirect’ instead of ‘init’.

    so like this:

    add_action( 'template_redirect', 'redirect_visitors' );

    @braun_trutta

    Participant

    DISCO! You’ve done it! Now to find out what template_redirect is? I suspect it fires after init and the page now knows more about what’s going on? Also, How would I modify this so that the non-logged in user is only redirected to the registration page if they visit the home page (in other words, I would like them to have access to the rest of the site)

    Thanks so much for your help!

    @tanner-m

    Participant

    Great, glad that fixed it! You can see the list of WP actions here: https://codex.wordpress.org/Plugin_API/Action_Reference.

    To limit this redirect just to the home page update it to something like this:

    add_action( 'template_redirect', 'redirect_visitors' );
    function redirect_visitors() {
        if ( !is_user_logged_in() && is_front_page() ) {
    	wp_redirect( site_url() . '/my-site-registration' );
            exit;
       }
    }

    @braun_trutta

    Participant

    Got it. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Redirect users to Registration other wise set Activity as front page’ is closed to new replies.
Skip to toolbar