To call page specific html.tpl.php document for Drupal 7, enter this code in the template.php document:

function THEMENAME_preprocess_html (&$vars) {
if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $template_filename = 'html';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '__' . $path_part;
        $vars['theme_hook_suggestions'][] = $template_filename;
      }
    }
  }
}
  • Duplicate the 'html.tpl.php' document and name it 'html--PAGENAME.tpl.php' (this example would be 'html--hook.tpl.php'), enter custom code and upload it to the server if working online
  • With safe URL's turned on, create a page ('hook' for instance). This would be the address 'http://www.example.com/hook'
  • Navigate to: 'Administration » Configuration » Development' and click 'Clear all caches'
  • Navigate to your page ('hook' in this example) and refresh the page

Comments

bahsite’s picture

If my url alias is hook-example, my html template would be 'html--hook-example.tpl.php', right?

Thanks for your help

WillGFP’s picture

I've been looking for a way to do this for a while. Your solution is not only simple, but also works perfectly. Thank you!

webalchemist’s picture

It Worked perfect for me, Thank YOU!!

xturgorex’s picture

My problem is I can't simply add this code. I get:

Cannot redeclare bartik_preprocess_html() (previously declared in themes/bartik/template.php:6) in themes/bartik/template.php on line 49

So, I am trying placing it in preprocess_html function like this...

function bartik_preprocess_html(&$variables) {
if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $template_filename = 'html';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '__' . $path_part;
        $vars['theme_hook_suggestions'][] = $template_filename;
      }
    }
  }
  if (!empty($variables['page']['featured'])) {
    $variables['classes_array'][] = 'featured';
  }

  if (!empty($variables['page']['triptych_first'])
    || !empty($variables['page']['triptych_middle'])
    || !empty($variables['page']['triptych_last'])) {
    $variables['classes_array'][] = 'triptych';
  }

  if (!empty($variables['page']['footer_firstcolumn'])
    || !empty($variables['page']['footer_secondcolumn'])
    || !empty($variables['page']['footer_thirdcolumn'])
    || !empty($variables['page']['footer_fourthcolumn'])) {
    $variables['classes_array'][] = 'footer-columns';
  }
  // Add conditional stylesheets for IE
  drupal_add_css(path_to_theme() . '/css/ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lte IE 7', '!IE' => FALSE), 'preprocess' => FALSE));
  drupal_add_css(path_to_theme() . '/css/ie6.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'IE 6', '!IE' => FALSE), 'preprocess' => FALSE));
}

Please pardon me, but I do not know PHP or Drupal development.
The template.php file is in the original Bartik theme folder too.. I can't add one to my subtheme folder because I Get the same error

bartik_preprocess_html() previously declared in...

Is there some place in particular I need to place this code?

Edit: Never mind, I got it. I'm always learning. I was not using the name of my sub theme!
Works great thank you! Now, would it be more Drupally to setup second template.php file in my subtheme folder with this modification in there instead of modifying the original? This method seems to work. Are there issues with it?

suntog’s picture

It worked on my first try, that usually never happens!

Drupalima’s picture

Thanks for posting this. This worked smoothly for me.

Delphine Lepers’s picture

That's not even necessary actually.
If you check the content of the $vars, you'll see that there is a number of suggestions already, just copy html.tpl.php in your base theme templates folder (create one if there is none) and rename it with the most appropriate suggestion, that tpl will be used instead.

bisonbleu’s picture

Delphine is right. I was able to override html.tpl.php in a Drupal 7.43 install for specific nodes with to following pattern:

html--node__1529.tpl.php

:~: Senior Drupal Site Builder

Rameez’s picture

How would you do it for login page? Thanks

bisonbleu’s picture