0

Fairly new to wordpress coding. I am hoping someone could guide me on this issue i am having. Currently i am creating a theme that uses buddypress plugin with my own custom defined user roles. Everything for custom user roles are in place as:

For custom roles i have added and is working as i expect it to work:

  • administrator // administrator;
  • business_owner // custom role who can edit all users except administrator
  • business_manager // custom role who can edit all users except admin and owner
  • top_level_staffs // custom role who can edit all users except admin, owner, manager
  • junior_staffs // custom role who can edit all users except admin, owner, manager and top level staffs.

these all users have access to wordpress backend and can edit users as per their capabilities.

I think buddypress doesnt recognize this. so in the wordpress backend these users can create, modify and delete users only of those who they can.

But as soon as they go to buddypress profile via frontend, a normal staff are able to edit administrator just by clicking the buddypress settings and profile tab!! - this is just wrong.

So inorder to solve this issue. i got these options and logic.

  • compare the current logged in user with displayed user profile.
  • once compared remove the buddypress specific menu tab like 'settings' and so on.

So here's what i have done In my theme, I've got function.php and buddypress.php (this is for showing buddypress page slightly different than page.php formats.

codes in buddypress.php

here the codes are not optimized but clearly explained so everyone can understand it easily whats going on

//codes in buddypress.php
// the function " bp_displayed_user_id(); " - only displays user id when used in buddypress.php but not in function.php!!!

$displayed_user_id = bp_displayed_user_id();
$displayed_user = new WP_User( $displayed_user_id );

if ( !empty( $displayed_user->roles ) && is_array( $displayed_user->roles ) ) {
foreach ( $displayed_user->roles as $displayed_user_role )
    if($displayed_user_role == 'administrator') {
        $displayed_user_role = 9;
    }elseif ($displayed_user_role == 'business_owner') {
        $displayed_user_role = 8;
    }elseif ($displayed_user_role == 'site_manager') {
        $displayed_user_role = 7;
    }elseif ($displayed_user_role == 'top_level_staffs') {
        $displayed_user_role = 6;
    }elseif ($displayed_user_role == 'junior_staffs') {
        $displayed_user_role = 5;
    }elseif ($displayed_user_role == 'editor') {
        $displayed_user_role = 4;
    }elseif ($displayed_user_role == 'author') {
        $displayed_user_role = 3;
    }elseif ($displayed_user_role == 'contributor') {
        $displayed_user_role = 2;
    }else{
        $displayed_user_role = 1; //for subscriber
    }
}

$current_user_id = get_current_user_id();
$current_user = new WP_User( $current_user_id );

if ( !empty( $current_user->roles ) && is_array( $current_user->roles ) ) {
foreach ( $current_user->roles as $current_user_role )
    if($current_user_role == 'administrator') {
        $current_user_role = 9;
    }elseif ($current_user_role == 'business_owner') {
        $current_user_role = 8;
    }elseif ($current_user_role == 'site_manager') {
        $current_user_role = 7;
    }elseif ($current_user_role == 'top_level_staffs') {
        $current_user_role = 6;
    }elseif ($current_user_role == 'junior_staffs') {
        $current_user_role = 5;
    }elseif ($current_user_role == 'editor') {
        $current_user_role = 4;
    }elseif ($current_user_role == 'author') {
        $current_user_role = 3;
    }elseif ($current_user_role == 'contributor') {
        $current_user_role = 2;
    }else{
        $current_user_role = 1; //for subscriber
    }
}

if($current_user_role < $displayed_user_role) {
function restrict_bp_settings() {
    global $bp;
    bp_core_remove_nav_item('settings');
}
add_action( 'bp_setup_nav', 'restrict_bp_settings'); // this action - only works if used in function.php but not in buddypress.php why!!!
}
get_header();

if (have_posts()) : 
    while (have_posts()) : the_post();?>
<?php get_template_part('content');

    endwhile;

    else :
        get_template_part('nothing');

    endif;

get_footer();

So how can i able to add_action via buddypress.php to make this work. OR how can i able to make bp_displayed_user_id(); display id using in function.php

if there is any other method or Any other similar suggestion, ideas presented would be great as well.

Thanks.

  • 1
    Yes, it is possible to add_action in a template file, but I think that it is not your real quesiton. Also, it seems that you are trying to remove a nav tab in user profile in order to block access to it for some users, but removing it doens't block the access. Not sure what you are asking. – cybmeta Mar 21 '16 at 16:46
  • also - any functions that use BuddyPress hooks should go in theme functions.php, plugins/bp-custom.php or a plugin. Not in buddypress.php. Info re bp-custom: codex.buddypress.org/themes/bp-custom-php – shanebp Mar 21 '16 at 22:05
  • i think i does block the access if i use this in function.php like this function restrict_bp_settings() { global $bp; bp_core_remove_nav_item('settings'); } add_action( 'bp_setup_nav', 'restrict_bp_settings'); } this code will block all the users including admin and will not have access to the settings menu. it will hide it and also if they try to access using url will also show them 404 page. but if i use this add action via buddypresss.php then it wont work. But i need to use this in buddypress.php since i need to get bp_displayed_user_id(); which can be – Bhupen Mar 22 '16 at 0:20
  • continue...done in buddypress.php since the function bp_displayed_user_id(); will only display in buddypress pages as per the buddypress codex. if i use this function in funtion.php then it will return only '0' so but i need this since i need to compare it with current user roles. like if current user is less than the displayed user then hide this. but this is the part where the add action hook doesnt work. I am going nuts been a week figuring out how to pull this off – Bhupen Mar 22 '16 at 0:24
0

As cybmeta said - removing the tab does not block access to functionality.

The quick solution is to leave all the tabs in place, regardless of a user's role.

Instead, add a conditional to an overloaded template.

For example, overload this template:

buddypress\bp-templates\bp-legacy\buddypress\members\single\profile\edit.php

And add a role-based conditional at the top. The conditional will control if the user sees the edit form or a message about permissions or a redirect or... .

  • i think this method sounds promising. I will try this option and let you know if it works thanks for the help.. – Bhupen Mar 22 '16 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.