{"id":64078,"date":"2011-11-17T10:00:19","date_gmt":"2011-11-17T15:00:19","guid":{"rendered":"http:\/\/wpmu.org\/?p=64078"},"modified":"2013-04-15T15:44:46","modified_gmt":"2013-04-15T19:44:46","slug":"how-to-add-profile-privacy-to-buddypress","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-add-profile-privacy-to-buddypress\/","title":{"rendered":"How to Add Profile Privacy to BuddyPress"},"content":{"rendered":"<p>Privacy controls are critical to any successful social network. Right now there are roughly zero plugin options for adding privacy to BuddyPress 1.5+. Some basic privacy controls are in <a href=\"http:\/\/buddypress.trac.wordpress.org\/ticket\/3695\" rel=\"noopener\" target=\"_blank\">discussion<\/a> for future versions of BuddyPress, but there aren&#8217;t any usable options yet. Here&#8217;s a trick that may tide you over in the meantime.<\/p>\n<h2>Restrict User Profiles Based on Friendship<\/h2>\n<p>This is a <a href=\"http:\/\/buddypress.org\/community\/groups\/miscellaneous\/forum\/topic\/buddypress-profile-privacy-for-non-friends\/\" rel=\"noopener\" target=\"_blank\">hack<\/a> that I drudged up from the BuddyPress forums, a solution originally <a href=\"https:\/\/wpmudev.com\/forums\/topic\/additional-buddypress-profile-privacy-settings\" target=\"_blank\" rel=\"noopener\">proposed<\/a> by our very own Richie_KS. It basically checks to see if users are friends before displaying a profile. Here&#8217;s how to use it:<\/p>\n<p><strong>1. Add this to your theme&#8217;s <em>functions.php<\/em> file:<\/strong><\/p>\n<pre>\/\/ ------- Check if member is a friend  ------------- \/\/\r\n\r\nfunction bp_displayed_user_is_friend() {\r\nglobal $bp;\r\nif ( bp_is_profile_component() || bp_is_member() ) {\r\nif ( ('is_friend' != BP_Friends_Friendship::check_is_friend( $bp-&gt;loggedin_user-&gt;id, $bp-&gt;displayed_user-&gt;id )) &amp;&amp; (bp_loggedin_user_id() != bp_displayed_user_id()) )\r\nif ( !is_super_admin( bp_loggedin_user_id() ) )\r\n\r\nreturn true;\r\n}\r\n}<\/pre>\n<p><strong>2. Create a non-friend template file:<\/strong><\/p>\n<p>Essentially that will look something like this, with your own edits, of course:<\/p>\n<p>{code type=php}<\/p>\n<div id=\"not_friend_message\">\n<p>You must be friends in order to access users&#8217; profiles.<\/p>\n<\/div>\n<p>Name it <em>\u201cnot-friend.php\u201d<\/em> and it in your BuddyPress theme folder at: <em>\/members\/single\/<\/em><\/p>\n<p><strong>3. Edit your theme file to include the friendship check:<\/strong><br \/>\nTo restrict profile info, open up <em>\/members\/single\/profile.php<\/em> and add this at the top of the page:<\/p>\n<p>{code type=php}<br \/>\n&lt;?php if ( bp_displayed_user_is_friend() ) : ?&gt;<\/p>\n<p>&lt;?php locate_template( array( &#8216;members\/single\/not-friend.php&#8217; ), true ) ?&gt;<\/p>\n<p>&lt;?php else : ?&gt;<\/p>\n<p>Then you&#8217;ll need to add this at the bottom of the page:<\/p>\n<p>{code type=php}&lt;?php endif; ?&gt;<\/p>\n<p>Here&#8217;s what it will look like with the code in place:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-64095\" title=\"not-friends\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2011\/11\/not-friends.jpg\" alt=\"Post image\" aria-hidden=\"true\" width=\"782\" height=\"424\" \/><\/p>\n<p>The downside of this is that it doesn&#8217;t allow users to set their own privacy levels. However, I do like that it is a simple solution that can be implemented on the theme-level, without having to save anything new to the database. It&#8217;s by no means a full privacy solution, but I hope this tutorial helps those of you in a pinch who need some basic profile privacy in place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to restrict user profiles based on friendship? This tutorial will show you how.<\/p>\n","protected":false},"author":4099,"featured_media":64103,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[235,263],"tags":[146],"tutorials_categories":[],"class_list":["post-64078","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-misc","category-tutorials","tag-privacy"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/64078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/4099"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=64078"}],"version-history":[{"count":1,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/64078\/revisions"}],"predecessor-version":[{"id":216480,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/64078\/revisions\/216480"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/64103"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=64078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=64078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=64078"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=64078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}