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.
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.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 getbp_displayed_user_id();
which can bebp_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