Friday, March 23, 2012

Adding viewport meta tag to Drupal 7 theme without overriding html.tpl.php

You can use drupal_add_html_head() for adding the viewport meta tag. In your theme template.php file, you can call hook_page_alter() for embeding meta.


function YOURTHEMENAME_page_alter($page) {
//
$viewport = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' =>  'viewport',
'content' =>  'width=device-width, initial-scale=1, maximum-scale=1'
)
);
drupal_add_html_head($viewport, 'viewport');
}

5 comments:

Anonymous said...

Thanks!

Anonymous said...

By the way, what's wrong with just adding it to the html.tpl.php file?

I hijacked the "Framework" theme to build my theme, and it simply places this in the head:

*meta name="viewport" content="width=device-width" /*

(braces changed to asterisks because the input box wouldn't allow the html)

Anonymous said...

@Anonymous : "By the way, what's wrong with just adding it to the html.tpl.php file?" : Easy, what about keeping this functionnality when changing your theme ?

Anonymous said...

Clearly this solution also is theme-specific. Your argument is invalid.

Nafis Islam said...

The combination of selling and graphic style experience in our team has been terribly undefeated. Our Drupal themes area unit stunning, and have nice rating.
graphics design
open source software
graphic design portfolio
responsive themes
drupal themes
graphic design portfolio examples
design portfolio
open source softwares
open source database
best graphic design