I would like to suppress all the PHP error messages (errors, notices) for a regular authenticated user, BUT display ALL of them for an administrator ($user->uid == 1 or user with an "administrator" role).

This way I have the opportunity to set "Error messages to display" to "None" at admin/config/development/logging, but unfortunately this is a GLOBAL setting, which is not the right one for me.


I haven't tested this but I would think you could turn that setting off completely, and enable it again yourself in a custom module:

function MYMODULE_init() {
  if (user_access('a permission only admins have')) {


Per your comments, it looks like this way doesn't work, but what does work is to do exactly the reverse of the above. That is, to set the error reporting to "All messages" in the UI, and then use hook_init() to surpress the unwanted levels of error message.

I would say your suspicions are correct, that Drupal is handling the errors at a lower level and so turning them off all together stops calling error_reporting() in hook_init() from having any effect.

| improve this answer | |
  • oh, why didn't it come into my mind earlier, now it seems like trivial... :D Thanks, Clive! But maybe it would be better to check if a user has a specific role (e.g. if (in_array('administrator', $user->roles))){...}), wouldn't it? OK, I'll try that soon, and give you a feedback. Until then, +1 for your answer! – Sk8erPeter Sep 22 '12 at 18:13
  • If you can get a solution working to your liking, I also think that hook_boot() may be a better place to do this. I am not positive, though, if user_access() and siblings work here, though. Using hook_boot() will allow you to detect problems in other hook_init()s. – mpdonadio Sep 22 '12 at 19:03
  • 2
    @Sk8erPeter Whichever one you prefer really :) The normal 'Drupal' way to do it is to check by permission, so that your code is reusable (if you decide to grant that access to another role in future for example). Either way is fine though – Clive Sep 22 '12 at 19:54
  • @MPD Yeah I wan't too sure about user_access() in hook_boot() either so I just went with hook_init() – Clive Sep 22 '12 at 19:55
  • @Clive: OK,I tried that,and it doesn't work if I set "Error messages to display" to "None",and set a higher error_reporting level later in hook_init(),possibly because Drupal's error handler suppresses all these error messages. But it DOES work "backwards"! So I set the previous option at admin/config/development/logging to "All messages", and after that, I checked if a user is NOT an administrator or does NOT have a right (e.g. if(!in_array('administrator', $user->roles))), and if NOT, then set error_reporting to a LOWER level (e.g. E_ALL & ~E_NOTICE, which takes out notices). – Sk8erPeter Sep 23 '12 at 0:49

Use my Error Level Permission module.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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