Buddypress Remove Profile tabs by Role

Hello,

is it possible to remove cover image and certain tabs from Buddypress profile page only for users of specific role?

Thank you!!

  • Rupok

    Hi Ioannis, hope you had a wonderful day.

    Yes, it's possible by creating a MU Plugin. For creating a MU Plugin, create a folder named "mu-plugins" inside your "wp-content" folder. Then inside that "mu-plugins" folder, create a file with any name and with a .php extension. Now paste the code inside that file and save.

    <?php
    add_action( 'wp_head', 'get_user_role' );
    function get_user_role() {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    if ($user_role == "administrator"){
    	?>
    	<style type="text/css">
    		.nav-menu {
    		  display: none;
    		}
    </style>
    <?php
    }elseif ($user_role == "Editor") {
    	# code...
    };
    };

    For getting more idea on mu plugins, you can go through this article: http://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    You can check for more user roles by adding "elseif" condition in the code I've provided. You will get more idea here: http://php.net/manual/en/control-structures.elseif.php

    And in the example, I've hiddedn the navigation menu box with ".nav-menu" class if currently logged in user is Administrator. You can hide/show different elements in the same way as you want.

    Please let us know if you have any confusion of if you are stuck at any point. We will be glad to help.

    Have a nice day. Cheers!
    Rupok

  • Ioannis

    Hello Rupok,

    thank you so much for the code. That is exactly what I was looking for but I have some issues.

    1) could you please give me an example with the else if statement because if I add css there my website crashes and appears only blank page. Here is the code I have added to "mu-plugins" folder

    <?php
    add_action( 'wp_head', 'get_user_role' );
    function get_user_role() {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    if ($user_role == "customer"){
    	?>
    	<style type="text/css">
    		#item-header-cover-image,.avatar,#item-header,#change-avatar-personal-li,#change-cover-image-personal-li,#members-followers-personal-li,#groups-personal-li,#appointments-personal-li{
        display: none;
    		}
    </style>
    <?php
    }elseif ($user_role == "subscriber") {
    	#mega-menu-wrap-before-header #mega-menu-before-header {
        background-color: red;}
    };
    };

    2) How could I make the tabs be hidden only in current members buddypress profile but be visible when the member visits the profiles of other users? A link to an example page mydomain.xx/massage-therapists/maria/

    Thank you,
    Ioannis

  • Rupok

    Hi Ioannis, hope you had a wonderful day.

    From your code, it seems like you missed ending closing php tags before starting HTML, also <style> tags on your "elseif" condition. Can you please try the following code and check if it works for you?

    <?php
    add_action( 'wp_head', 'get_user_role' );
    function get_user_role() {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    	if ($user_role == "customer"){
    	?>
    	<style type="text/css">
    		#item-header-cover-image,.avatar,#item-header,#change-avatar-personal-li,#change-cover-image-personal-li,#members-followers-personal-li,#groups-personal-li,#appointments-personal-li {
    			display: none;
    		}
    	</style>
    	<?php
    	}elseif ($user_role == "subscriber") {
    	?>
    	<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: red;
    		}
    	</style>
    	<?php
    	};
    };

    You can check if logged in user is on his own profile or not with "bp_is_my_profile();" function. Then run your custom CSS depending on that condition. You can get more details about this function here: http://hookr.io/functions/bp_is_my_profile/

    Please let us know if you have any confusion. We will be glad to help further.

    Have a nice day. Cheers!
    Rupok

  • Ioannis

    Hello Rupok,

    thank you for the updated code but I still have some issues:

    1) If I place the function "bp_is_my_profile();" in the above code the wesite crashes and does not work at all.

    <?php
    add_action( 'wp_head', 'get_user_role' );
    function get_user_role() {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    	if ($user_role == "customer"){function bp_is_my_profile() {
        if ( is_user_logged_in() && bp_loggedin_user_id() == bp_displayed_user_id() ) {
            $my_profile = true;
        } else {
            $my_profile = false;
        }
    	?>
    	<style type="text/css">
    		#item-header-cover-image,.avatar,#item-header,#change-avatar-personal-li,#change-cover-image-personal-li,#book-appointment-personal-li,#location-personal-li,#members-followers-personal-li,#groups-personal-li,#buddyblog-personal-li,#appointments-personal-li {
    			display: none;}
    
    	</style>}
    	<?php
    	}elseif ($user_role == "editor") {
    	?>
    	<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: red;
    		}
    	</style>
    	<?php
    	};
    };

    2) The code you gave me works very well with 1 elseif but if I add more elseif statements the website crashes and appears only blank page

    <?php
    add_action( 'wp_head', 'get_user_role' );
    function get_user_role() {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    	if ($user_role == "customer"){
    	?>
    	<style type="text/css">
    		#item-header-cover-image,.avatar,#item-header,#change-avatar-personal-li,#change-cover-image-personal-li,#members-followers-personal-li,#groups-personal-li,#appointments-personal-li {
    			display: none;
    		}
    	</style>
    	<?php
    	}elseif ($user_role == "subscriber") {
    	?>
    	<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: red;
    		}
    	</style>
    	<?php
    
    </style>
    	<?php
    	}elseif ($user_role == "editor") {
    	?>
    	<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			display: none!important;
    		}
    	</style>
    	<?php
    
    	};
    };

    Could you please add "bp_is_my_profile();" function only in the first if statement and add 2 or 3 more elseif statements as an example? I do not have knowledge of php code so that would be too helpfull to me!!

    Thank you a lot,
    Ioannis

  • Rupok

    Hi Ioannis, hope you had a wonderful day.

    Initially I thought you are trying to get specific user role, but as you have multiple cases, can you try PHP switch instead of if statement? You will get more idea about PHP Switch case here: http://www.w3schools.com/php/php_switch.asp

    I could try writing full code for you, but I exactly don't know how many cases you have and what are your desired output for each case. So it's better if you can write this. That will help you if future if you add more user roles.

    There are syntax error in your first code block. It should be like this:

    <?php
    add_action( 'wp_head', 'get_user_role' );
    function get_user_role() {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    	if ($user_role == "customer"){
    		function bp_is_my_profile() {
    		    if ( is_user_logged_in() && bp_loggedin_user_id() == bp_displayed_user_id() ) {
    		        $my_profile = true;
    		    } else {
    		        $my_profile = false;
    		    };
    		};
    	?>
    	<style type="text/css">
    		#item-header-cover-image,.avatar,#item-header,#change-avatar-personal-li,#change-cover-image-personal-li,#book-appointment-personal-li,#location-personal-li,#members-followers-personal-li,#groups-personal-li,#buddyblog-personal-li,#appointments-personal-li {
    			display: none;}
    
    	</style>}
    	<?php
    	}elseif ($user_role == "editor") {
    	?>
    	<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: red;
    		}
    	</style>
    	<?php
    	};
    };

    There is also error in your second code block. It should be this:

    <?php
    add_action( 'wp_head', 'get_user_role' );
    function get_user_role() {
    	global $current_user;
    	$user_roles = $current_user->roles;
    	$user_role = array_shift($user_roles);
    	if ($user_role == "customer"){
    	?>
    	<style type="text/css">
    		#item-header-cover-image,.avatar,#item-header,#change-avatar-personal-li,#change-cover-image-personal-li,#members-followers-personal-li,#groups-personal-li,#appointments-personal-li {
    			display: none;
    		}
    	</style>
    	<?php
    	}elseif ($user_role == "subscriber") {
    	?>
    	<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: red;
    		}
    	</style>
    	<?php
    	}elseif ($user_role == "editor") {
    	?>
    	<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			display: none!important;
    		}
    	</style>
    	<?php
    
    	};
    };

    Please let us know if you have any further query. We will be glad to help.

    Have a nice day. Cheers!
    Rupok

  • Ioannis

    Hello again Rupok,

    Thank you for the updated codes but unfortunately the website still crashes with the first code and blank page appears.

    The roles that I have in my website are customer, s2member_level1, s2member_level2, s2member_level3 and admin. I would like to

    1) hide specific elements for each role in curent user's profile
    2) hide specific elements for each role general in website

    I have read the article about "php switch" but I don't know how to add user's roles in the code or insert css . Is it possible to give me an example either with the above code or "php switch"?

    Thank you a lot,
    Ioannis

  • Kasia Swiderska

    Hello Ioannis,

    If page crashes then you have to debug it to see what is exact error. To enable debug, open your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

    define('WP_DEBUG', true);

    In order to enable the error logging to a file on the server you need to add yet one more similar line:

    define( 'WP_DEBUG_LOG', true );

    In this case the errors will be saved to a debug.log log file inside the /wp-content/directory.

    Depending on whether you want your errors to be only logged or also displayed on the screen you should also have this line there, immediately after the line mentioned above:

    define( 'WP_DEBUG_DISPLAY', false );

    The wp-config.php is located in your WordPress root directory. It’s the same file where the database configuration settings are. You will have to access it by FTP or SFTP in order to edit it.

    Check then debug.log and see what error is your code throwing and then you will see what need to be fixed.

    This is example on how to use switch with your roles

    <?php
    	$user_role = array_shift($user_roles);
    
    switch ($user_role ) {
        case "customer":
            echo '<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: red;
    		}
    	</style>';
            break;
        case "s2member_level1":
                  echo '<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: blue;
    		}
    	</style>';
            break;
        default:
                   echo '<style type="text/css">
    		#mega-menu-wrap-before-header #mega-menu-before-header {
    			background-color: black;
    		}
    	</style>';
    }
    ?>

    Please note this is only part of the code. As we are very happy to help you by pointing you in right direction writing whole script for you would be custom coding, and this is bit beyond support forum scope http://premium.wpmudev.org/manuals/using-wpmu-dev/support-vs-custom-development/

    kind regards,
    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.