Created
April 8, 2011 05:11
-
-
Save mattwiebe/909331 to your computer and use it in GitHub Desktop.
Conditional <html> for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The WordPress answer to: | |
* @link paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ | |
* @author Matt Wiebe | |
* @link http://somadesign.ca/ | |
* @var $min int minimum version of IE to target. | |
* @var $max int maximum version of IE to target - will receive no IE-specific classes. | |
*/ | |
function sd_conditional_html_elements( $min = 7, $max = 9 ) { | |
$i = $min; | |
ob_start(); | |
language_attributes(); | |
$attr = ob_get_clean(); | |
$html = '<html class="no-js%s" '. $attr .'>'; | |
$comments = '<!--[if %s %s]> %s <![endif]-->'."\n"; | |
while ( $i < $max ) { $j = $i; //cache for second while loop | |
$c = array( 'ie'.$i ); | |
$ie = ( $i !== $min ) ? 'IE' : 'lte IE'; | |
while ( $j < $max ) { $j++; | |
$c[] = 'ie-lt'.$j; | |
} | |
$classes = ' '.implode( ' ', $c ); | |
printf( $comments, $ie, $i, sprintf( $html, $classes ) ); | |
$i ++; | |
} | |
printf( "<!--[if (gte IE %s)|!(IE)]><!--> %s <!--<![endif]-->\n", $max, sprintf($html,'') ); | |
} |
I wouldn't use this to teach someone PHP, but it does the job, so that's enough for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It actually does not seem great to me, because it is super hard to read.
I would drop most PHP out of it, and just use as close to plain HTML as possible. Alternatively I might refactor it to a point where it reads like english as much as possible. So, basically, there would be no $j and $i, etc.