Skip to:
Content
Pages
Categories
Search
Top
Bottom

I Need Help Writing A Custom Function to Redirect Non-Logged in Users.


  • foodin65
    Participant

    @foodin65

    First off, I’ve just installed Justin Tadock’s Members Plugin to get access to the Restrict Roles Template tags. What I’d like to do is make it so that specific components of my BP install are only available to certain roles.

    This is the code I’ve come up with so far:

    if ( bp_is_active( ‘profile’ | ‘activity’ | ‘messages’ | ‘friends’ | ‘groups’ | ‘settings’ ) && function_exists( ‘current_user_has_role’ ) && current_user_has_role( ‘subscriber’ | ‘moderator’ | ‘administrator’ ) ) {
    // the BP Component is “Profile, Activity, Messages, Friends, Groups, or Settings”, and the users role is “Subscriber, Moderator, or Administrator”

    display the component; // I don’t know what to put here so that the component is displayed if the user has the right role.
    }

    else {
    wp_redirect(get_option(‘siteurl’) . ‘/page-of-my-choice.php’);; // redirects user who don’t have the correct role to my page of my choice
    }

    Any Ideas on how to make this work?

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

  • foodin65
    Participant

    @foodin65

    This is another Idea i have:

    function bp_is_active( ‘profile’ || ‘activity’ || ‘messages’ || ‘friends’ || ‘groups’ || ‘settings’ ) && function_exists( ‘current_user_has_role’ ) && current_user_has_role( ‘subscriber’ || ‘moderator’ || ‘administrator’ ) {
    global $bp;

    if ( BP_ACTIVATION_SLUG == $bp->current_component )
    return true;
    if (wp_redirect(get_option(‘siteurl’) . ‘/wp-login.php’))
    return false;
    }

    But again it doesn’t work…. am I even on the right track?


    r-a-y
    Keymaster

    @r-a-y

    I’m quite sure you can’t tack on just any component in the bp_is_active() function.

    It should be like this:
    if ( bp_is_active('profile') || bp_is_active('activity') || ... etc ) {

    The easiest thing would be to list what you want publically available.

    Here’s a function to build on:
    https://buddypress.org/community/groups/how-to-and-troubleshooting/forum/topic/how-to-make-a-private-community/?topic_page=2&num=15#post-44616


    techguy
    Participant

    @crashutah

    You should use bp_core_redirect() instead of wp_redirect().

    Yeah, bp_is_active() only take a single argument.

    It looks like you have a solution, so for those of us who need some additional coaching, can you please explain how to implement it?

    I just love what BuddyPress has to offer, however for privacy reasons, on the site I’m developing I need to hide several components like “Activity” and “Members” when a visitor is not logged in.

    Thanks!


    r-a-y
    Keymaster

    @r-a-y

    @jbarr – I linked to a solution above. Please take a look and try it out! Paste the snippet in your theme’s functions.php file.

    when I add the function to functions.php in my theme directory, when i navigate to any page of my site, it just comes up blank. Suggestion?


    r-a-y
    Keymaster

    @r-a-y

    Just tried it on my install and it works.

    Can you paste the full contents of your functions.php on Pastebin?

    The source is here: http://pastebin.com/LSghtFLW

    The added function is at the bottom.

    I am using the CosmicBuddy BP theme.

    Just to clarify, the function code above is added to the functions.php in the theme’s directory (for example, /wp-content/themes/cosmicbuddy/) and NOT in the /wp-includes/ directory, correct?

    You’re not simply copying the original functions file to your child theme though are you? functions file in a child theme does not overule the original, both are parsed, so you mustn’t duplicate the original files functions. The new child theme functions file must only contain new functions.

    As far as I recall, everything is “stock” except for me inserting the above function code into the end of the /wp-content/themes/cosmicbuddy/functions.php file.

    Should I be adding the function code to the functions.php file in the /wp-content/themes// directory?

    Ok, I got it to work. I added this…

    function sh_walled_garden()
    {
    global $bp;
    if ( bp_is_register_page() || bp_is_activation_page() )
    return;
    if( ! bp_is_blog_page() && ! is_user_logged_in() )
    bp_core_redirect( $bp->root_domain .’/’. BP_REGISTER_SLUG );
    }
    add_action( ‘walled_garden’, ‘sh_walled_garden’ );

    …into /wp-content/plugins/buddypress/bp-themes/bp-default/functions.php

    and added…

    …into the top of /wp-content/plugins/buddypress/bp-themes/bp-default/header.php

    So far, so good!


    r-a-y
    Keymaster

    @r-a-y

    Hmm… you shouldn’t need to add this to /bp-themes/bp-default/, if you upgrade, you’ll lose your changes. Try using the same snippets in Cosmic Buddy’s functions.php and header.php.

    Anyway, Cosmic Buddy is not a child theme of the BP default theme, so I don’t see why it would work if you paste it in /bp-themes/bp-default/.

    Sorry for the confusion. In testing, I reverted back to the stock, default theme to make sure there wasn’t anything else at issue. That worked, so I then made the update to functions.php and header.php in the Cosmic Buddy directory, and it now works properly. Fortunately, I’m doing all this on a test install, so it’s some good lessons learned!

    Thanks for your help!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘I Need Help Writing A Custom Function to Redirect Non-Logged in Users.’ is closed to new replies.
Skip to toolbar