Skip to:
Content
Pages
Categories
Search
Top
Bottom

Disallow Non logged users on certain pages


  • saish_neugi
    Participant

    @saish_neugi

    Hello everyone, I am facing with this issue from quite a few days. I am a newbie to wordpress/buddypress and presently i am developing a site using buddypress. The issue i am facing is similar to what discussed in this but as you can see it dont give a full fledged solution until end. I want to restrict the non logged in users to view only the register and login page and they shouldnt be able to view any other page created on the fly.

    Any suggestions are welcomed.

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

  • chatty24
    Participant

    @chatty24

    Try This Code :

    add_action( 'wp', 'custom_lockdown_redirect', 3 );
    function custom_lockdown_redirect(){
    global $wp;
    	if (!is_user_logged_in()){
    	if ( bp_is_activation_page()
    		|| bp_is_register_page()
    		|| ( in_array( $GLOBALS['pagenow'], array( '/login' )))
    		)
    	 return;
    		bp_core_redirect(get_option('http://yoursite.com') . "/login");
    		exit;
    		}
    	}

    Make sure the you change /login with your login link.

    Add it to your theme’s function.php file


    saish_neugi
    Participant

    @saish_neugi

    Hello @chatty24 . . thanks for stopping by and luking into the problem. As u suggested, i have included your code in my bp-custom.php. it does prevent it from going to any other page but it ends up going on to a page not found error and i found that its not pointing to the exact url even after changing to my custom link.


    chatty24
    Participant

    @chatty24

    Try to add it to your theme’s function.php file instead of bp-custom.php


    saish_neugi
    Participant

    @saish_neugi

    I tried adding it even into functions.php but it still has the same behavior. It is unable to find the Server path. Instead it directly points to my page. For example my path is localhost/expo_social/welcome but it redirects to localhost/welcome thereby showing the page not found error.


    saish_neugi
    Participant

    @saish_neugi

    and one more thing. . i managed to sort the above error by just putting two dots before my page url. but the wiered thing is that when i try to login, it again takes me to my register page 😀


    saish_neugi
    Participant

    @saish_neugi

    @chatty24 is there anything else i can try in this?


    mrjarbenne
    Participant

    @mrjarbenne

    This is what I use. It blocks access to all the BP and bbPress components, but allows visitors to see the blog. I’m sure you could customize it to kill access to that too.

    When you attempt to access a page you shouldn’t be able to see, you are redirected back to the home page

    /**
     * Block public access to site with some exceptions.
     */
    function hwdsb_block_access() {
    	if ( is_user_logged_in() ) {
    		return;
    	}
    
    	$redirect = is_buddypress();
    
    	$site_path = bp_core_get_site_path();
    
    	// subdirectory install
    	if ( is_multisite() && ! is_subdomain_install() ) {
    		global $current_blog;
    		$site_path = $current_blog->path;
    	}
    
    	// strip site path from URI
    	$path = substr( esc_url( $_SERVER['REQUEST_URI'] ), strlen( $site_path ) );
    
    	if ( ! $redirect && function_exists( 'is_bbpress' ) ) {
    		$redirect = is_bbpress();
    	}
    
    	// redirect if we match a condition and we're not on the homepage
    	if ( (bool) $redirect === true && $path != '' ) {
    		bp_core_redirect( bp_core_get_root_domain() );
    		exit();
    	}
    
    }
    add_action( 'template_redirect', 'hwdsb_block_access', 0 );

    saish_neugi
    Participant

    @saish_neugi

    @mrjarbenne this is what exactly i wanted. . thnx pal. .


    stefcompeers
    Participant

    @stefcompeers

    Great! Found where I was looking for…

    BUT: we use Buddypress Docs, and the page /docs is still accessible…

    Any suggestions?

    THX!!!


    gbsu
    Participant

    @gbsu

    @mrjarbenne

    I am using the code snipet you advise to block access to all the BP and bbPress components, but allows visitors to see the blog. It seems that this code re-directs back to homepage. I am interested in having visitors re-directed to another page, in my situation, to my sign up page. Can you advise where in the code I can make such adjustment as well as advise the code to use, thanks.


    saish_neugi
    Participant

    @saish_neugi

    @gbsu
    change this line bp_core_redirect( bp_core_get_root_domain() );

    to where you want your page to redirect
    for eg.

    bp_core_redirect( bp_core_get_root_domain()."/sign-up");


    gbsu
    Participant

    @gbsu

    @saish_neugi

    Thanks, that did it.


    RomanSB
    Participant

    @romansb

    @saish_neugi the Plugin Private Buddypress works well also. It isn’t being updated anymore, but I’ve tested it and still works without errors.


    saish_neugi
    Participant

    @saish_neugi

    @romansb that can also be a gud way. . only to take care that it doesn’t temper some of your plugins functioning as its a very old plugin.


    Projekt-42
    Participant

    @projekt-42

    A hassle-free solution is to use a theme with a Restrict Page / Require A Login page template.


    djsteveb
    Participant

    @djsteveb

    Isn’t this something that “press permit core” plugin does out of the box? Maybe I am missing what is needed here.


    kykolka
    Participant

    @kykolka

    chatty’s code worked for us – thanks

Viewing 17 replies - 1 through 17 (of 17 total)
  • The topic ‘Disallow Non logged users on certain pages’ is closed to new replies.
Skip to toolbar