Privacy conditions for Drupal Custom Profile Fields

On a recent project we wanted to give users the ability to enter information but then have check boxes to enable or hide profile information.

I can elaborate if someone wants more information but for now here are the real basic steps and the code snippet.

Creating the Profile fields

We created a custom profile text field called "name" for their real name.
We created a checkbox field called "showname" with help instructions to explain that checking the box would make it visible to other registered users.

Modifying User Profile

  • Copy user-profile.tpl.php from the Drupal core to your theme folder.
  • Add the following code

    <?php if ($account->profile_showname == '1') {print $account->profile_name;}; ?>

    Please share this if it was helpful, tell us if it wasn't (or needs more info).

    P.S. to include a profile field without any conditions you normally use:
    <?php print $account->profile_name?>

Add new comment