Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect if not logged in and not on register page


  • 0901091
    Participant

    @0901091-1

    So far I have

    <?php
    function my_redirecter() {
    global $bp;
        if ( ! is_user_logged_in()&& BP_REGISTER_SLUG == $bp->current_component)
    	{
            wp_redirect( home_url() . '/register/');
    		exit();
        }
    	else
    	{
    	}
    }
    add_action( 'init', 'my_redirecter' );
    ?>

    I want to redirect to register page if not logged in AND not already on register page (otherwise I have infinite redirects).

    It seems that the $bp->current_component part isn’t right. My slug inside wp-config.php looks like:

    define ( ‘BP_REGISTER_SLUG’, ‘register’ );

    Any tips?

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

  • 0901091
    Participant

    @0901091-1

    I know I’m missing a ‘!’ – still doesn’t work with it though


    danbp
    Participant

    @danbp

    try without space:
    if ( !is_user_logged_in


    bp-help
    Participant

    @bphelp

    @0901091-1
    Untested but you can try this:

    
    <?php
    function my_redirecter() {
        if ( ! is_user_logged_in() && !bp_is_register_page() )
    	{
            wp_redirect( home_url() . '/register/');
    		exit();
        }
    	else
    	{
    	}
    }
    add_action( 'init', 'my_redirecter' );
    ?>
    

    Not sure what you wanted to do with the else so I left it there.


    0901091
    Participant

    @0901091-1

    @bp-help

    Thanks, that worked. Any idea how I could test if on homepage, /home/, rather than register?

    home slug: define(BP_HOME_BLOG_SLUG,’home’)


    bp-help
    Participant

    @bphelp

    @0901091-1
    Not sure if this is exactly what you need and I didn’t test it but you can try it out:

    
    <?php
    function my_redirecter() {
        if ( !is_user_logged_in() && !is_home() )
    	{
            wp_redirect( home_url() );
    		exit();
        }
    	else
    	{
    	}
    }
    add_action( 'init', 'my_redirecter' );
    ?>
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Redirect if not logged in and not on register page’ is closed to new replies.
Skip to toolbar