I need to add a body class to the page when either a 403 or 404 error is served, I'm not sure how to go about this. I'm using logintoboggan which sets the default 403 page to toboggan/denied so don't want to override that and break functionality.

Any pointers would be great.

3 Answers 3


You can implement hook_preprocess_page(), and alter the value of $variables['body_classes'], which contains a list of CSS classes separated with a comma.
In Drupal 6, code similar to the following one should work:

function mymodule_preprocess_page(&$variables) {
  $headers = drupal_get_headers();

  if (preg_match('@HTTP/1\.[01]\x20+403@', $headers)) {
    $variables['body_classes'] .= 'error-403,';
  elseif (preg_match('@HTTP/1\.[01]\x20+404@', $headers)) {
    $variables['body_classes'] .= 'error-404,';

Replace "error-403" and "error-404" with the actual CSS classes you want to use.

This is similar to the code I have used in a module hosted on Drupal.org, which had to detect when Drupal 6 shown an "access denied" page, or a "page not found" page.

  • 1
    I would imagine that you could also use menu_get_active_item and compare its path to drupal_get_normal_path(variable_get('site_403', '')); if you knew that a 404 page had been set.
    – Angry Dan
    May 20, 2013 at 11:59

For anyone working with Drupal 7, the function names and the way you pass variables are a bit different. The D7 solution for a 404 would look like this:

function MYMODULE_preprocess_html(&$variables) {
  $headers = drupal_get_http_header();

  if (isset($headers['status']) && $headers['status'] == '404 Not Found') {
    $variables['classes_array'][] = 'page-404';

The drupal_get_headers() function from D6 is now drupal_get_http_header() in D7. The values returned are structured a bit differently. You can view the contents of the array with "dpm($headers);" if you're using the devel module.

Finally, additional class variables can be added as elements of the $variables['classes_array'] by assigning them to an empty index, which will then be added to the html body tag via the template_process() function.

  • It can be directly "drupal_get_http_header("status");" if there is no need for all others header values Jul 5, 2017 at 9:50

I've created a custom module in order to have a custom twig template for my 404 error page.

I needed to have a custom body class for this page for theming purpose.

Here is what I've done :

function MYTHEME_preprocess_html(&$variables) {

    // Check if current request is an exception to get error type
    $status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');

    // add body classes.
    if ($status && $status->getStatusCode() == 404) {
        $variables['attributes']['class'][] = 'path-error-404';

    if ($status && $status->getStatusCode() == 403) {
        $variables['attributes']['class'][] = 'path-error-404';


  • Exactly what I was looking for. Thanks! Mar 31, 2022 at 15:56

Your Answer

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

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