Skip to:
Content
Pages
Categories
Search
Top
Bottom

redirect – if not logged in – for some sections

  • @shanebp

    Moderator

    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

    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.

    @davidtcarson

    Participant

    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

    Perfect – Thanks David

    @yeshourun

    Participant

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

    @yeshourun

    Participant

    Forget it. I found it! 😀

    @bphelp

    Participant

    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.

    @yeshourun

    Participant

    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

    @yeshourun

    Participant

    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