Optimise your Drupal Theme for an iPhone or iPod

Added on 30th November 2010

To begin with, enter the following code at the top of your page.tpl.php file:

 

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }<?php
if (ereg('iPhone',$_SERVER['HTTP_USER_AGENT'])) {
$iphone = 1;
} elseif (ereg('iPod',$_SERVER['HTTP_USER_AGENT'])) {
$iphone = 1;
} else {
$iphone = 0;
}
?>{/syntaxhighlighter}

This creates an 'iPhone' variable, and sets its value to 1 if the an iPhone is being used. The value of the variable can then be used in an 'if' statement to create content that's only shown if an iPhone is being used.

For example:

 

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }<title>
<?php if ($iphone == '0') {
print $head_title;
} else {
print $site_name;
} ?>
</title>{/syntaxhighlighter}

If the variable 'iPhone' equals 0 (if it is not an iPhone), then print the head title. Else (if it is an iPhone), print just the site name.

Add new comment