Skip to:
Content
Pages
Categories
Search
Top
Bottom

redirect – if not logged in – for some sections


  • shanebp
    Moderator

    @shanebp

    None of the redirect plugins (that I can find) work.
    Or they aren’t flexible enough.

    So I’m trying to write a function for child theme/functions.php

    I need to redirect users away from some parts of the site – if they are not logged in.

    I can use :
    is_user_logged_in()
    to find out if they are logged in.

    If they aren’t – any hints on how to find out what section they are in?
    For example, sections like: Groups, Members, etc

    If they are in a ‘private’ area, then I can send them to the register / login page.

    I guess I could use parse_url and some string comparing – but hoping there is a BP native way.

    Thx.

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

  • shanebp
    Moderator

    @shanebp

    Tried using get_permalink.
    It worked for nav buttons, but any sidebar icons, links, etc returned the home page post from get_permalink although they went to the correct page as shown in the browser url.

    Could not figure that out, so I just went with some basic php in bp-custom.php using $_SERVER and meta refresh.


    David Carson
    Participant

    @davidtcarson

    There is an epic thread on this that is worth a read – https://buddypress.org/community/groups/creating-extending/forum/topic/securing-components-from-non-logged-in-users/

    That thread has lots of examples and discussion.

    Here’s an example:

    `
    function restrict_access(){
    global $bp, $bp_unfiltered_uri;

    if (!is_user_logged_in() &&
    (
    BP_MEMBERS_SLUG == $bp_unfiltered_uri[0] ||
    BP_GROUPS_SLUG == $bp->current_component ||
    BP_BLOGS_SLUG == $bp->current_component ||
    ‘forums’ == $bp->current_component ||
    is_page_template(‘private-page.php’)
    )
    ) {

    bp_core_redirect( get_option(‘home’) . “/private/” );

    }
    }

    add_action( ‘wp’, ‘restrict_access’, 3 );
    `

    You can put that in bp-custom.php in the `wp-content/plugins/` directory. https://codex.buddypress.org/extending-buddypress/bp-custom-php/


    shanebp
    Moderator

    @shanebp

    Perfect – Thanks David


    freddy mcbob
    Participant

    @yeshourun

    i cant seem to find my bp-custom.php file… Any ideas?


    freddy mcbob
    Participant

    @yeshourun

    Forget it. I found it! 😀


    bp-help
    Participant

    @bphelp

    You have to create it bp-custom.php and put it in yoursite/wp-content/plugins
    Dont forget to add the opening and closing php tags in the bp-custom.php file you created.


    freddy mcbob
    Participant

    @yeshourun

    it doesnt work for me… this is what it says on my website: Parse error: syntax error, unexpected ‘<' in /wp-content/plugins/bp-custom.php on line 2


    freddy mcbob
    Participant

    @yeshourun

    this is what i have for my bp-custom.php file:
    `<?php

    function restrict_access(){
    global $bp, $bp_unfiltered_uri;

    if (!is_user_logged_in() &&
    (
    BP_MEMBERS_SLUG == $bp_unfiltered_uri[0] ||
    BP_GROUPS_SLUG == $bp->current_component ||
    BP_BLOGS_SLUG == $bp->current_component ||
    ‘forums’ == $bp->current_component ||
    is_page_template(‘website here’)
    )
    ) {

    bp_core_redirect( get_option(‘home’) . “/private/” );

    }
    }

    add_action( ‘wp’, ‘restrict_access’, 3 );

    ?>`

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘redirect – if not logged in – for some sections’ is closed to new replies.
Skip to toolbar