3

What I want to achieve is to hide the "Your name" field in comment form from all anonymous and authenticated users but visible to users with the admin role.

I was able to use CSS to hide the field, but it also hides from admin.

Please is there a way to selectively hide the field to anonymous and authenticated users but not to administrators?

2 Answers 2

6
function YOURMODULE_form_comment_form_alter(&$form, &$form_state) {
  global $user;

  if ($user->uid != 1) {
    unset($form['author']['name']);
  }

}

Another way is, if your theme has a comment.tpl.php file, putting a condition to restrict authenticated and anonymous user.

<?php print $author; ?> 

To limit this to a particular content type, you can create a separate comment template file for that content type.

1
  • Thank you. I used the first solution and it works! Please, is there a line to add to the code to limit this to a particular content type e.g article content type?
    – seyi
    Dec 18, 2015 at 12:24
4

If you have more than one users having administrator role then you may use this snippet. Place this piece of code at the bottom of template.php of your current theme.

Note: Replace YOUR_THEMENAME with the name of your current theme and YOUR_FORM_ID_HERE with id of your form.

function YOUR_THEMENAME_form_alter(&$form, &$form_state, $form_id) {
    switch($form_id){
    case 'YOUR_FORM_ID_HERE':
      global $user;
      if (!in_array('administrator', $user->roles)) {
          unset($form['author']['name']);
      }

    break;
  }
}
1
  • @seyi this solution is better as it will work for all the admin users, not just only one user with uid 1.
    – Dev
    Aug 11, 2016 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.