allow users to choose whether certain profile fields are visible or hidden
description
This handbook page explains how to allow your users to specify on their own account pages, which profile fields are visible or hidden when someone else is viewing their profile page.
example application
As an example, it might be useful if you are running a membership site where anonymous users can view profiles and you would like to offer the choice to members (registered users) whether their mobile phone number is visible to anonymous users or not.
usage
- This snippet is for use in your user_profile.tpl.php page override
- Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the snippet into your user_profile.tpl.php file.
Step 1 of 2
Using the example of "Do you want to hide your mobile phone number from non-members?" follow these steps.
- Click through to ADMINISTER -> SETTINGS -> PROFILES
- Below the ADD NEW FIELD option, click on CHECKBOX
- Make the Form Name
profile_hide_mobile - Fill out the other fields to suit. (Tip: adjust the weight of this custom field so the checkbox appears just below the mobile phone number field on the user EDIT ACCOUNT page.)
- Save the field
Step 2 of 2
Use this snippet to show/hide the mobile phone number (where the fieldname for the mobile phone number is profile_mobile).
<?php if ($user->profile_mobile): /*check to see if the user has entered a mobile field. displays nothing if it is blank */ ?>
<?php if (($GLOBALS['user']->uid) || (!$user->profile_hide_mobile) == '1') : /* check to see if the user viewing the page is logged in OR if the hide mobile field is selected */ ?>
<div class= "fields">
<P>Mobile phone number: <?php print ($user->profile_mobile) ; /* display this users mobile phone number */?></p>
</div>
<?php endif ; /*end of show or hide check */?>
<?php endif ; /*end of snippet */?>concise version of the same snippet without comments
<?php if (($user->profile_mobile) && (($GLOBALS['user']->uid) || (!$user->profile_hide_mobile) == '1')) : ?>
<div class= "fields">
<P>Mobile phone number: <?php print ($user->profile_mobile) ;?></p>
</div>
<?php endif ; ?>Notes
- the same snippet maybe used repeatedly within the same user_profile.tpl.php file or profile_profile.tpl.php (user list page override) file. Just edit the profile field names to suit
