Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The instructions for implementing Modernizr state that I should add class="no-js" to the <html> element.

Is there a way to do this in WordPress using a hook or filter? If at all possible, I'd prefer to do this without editing the theme files.

share|improve this question
Sorry, I fail at previewing and escaping my code - I need to add the class to the <html> element (see edited question). Also, I need to add it via PHP. "no-js" stands for "No Javascript", so using javascript to set that class is not an option. – Tex Jun 6 '11 at 8:08
Sorry about that. – Bainternet Jun 6 '11 at 9:24
This thread is a bit outdated, sorry, but may this plugin be usefull for someone reading you, as i did:… Enjoy ! – KouiK Dec 9 '11 at 14:44

1 Answer 1

This is not exactly the answer, but you can use a hook for language_attributes filter. This action is fired at <html> tag and what it does is simply echo the lang=en string, for ex. You can hook to that and replace the string with your CSS class, like this:

add_filter('language_attributes', 'modernizr');
function modernizr($output) {
    return $output . ' class="no-js"';

This works only when your theme follows the WordPress Theme Development Checklist. Sometimes people don't follow this and that breaks the technique.

share|improve this answer
I'd considered that, but it looks a little hacktacular, so I'm hoping to find another solution. Will leave the question open to see if there are any other interesting suggestions. – Tex Jun 6 '11 at 8:52
@Tex Take a look at the Codex. @rilwis suggestion is best practice. Place his code in your functions.php file. – kaiser Jun 6 '11 at 11:15
@kaiser I'm familiar with that article, but I don't see anything in the article that suggests that using language_attributes to set other attributes in the <html> tag is 'best practice'. As a matter of fact, the article states that the two acceptable parameters to the function are 'xhtml' and 'html', which supports my contention that this is a bit hacky. Anyway, if it works without detrimental side effects and it's the only way, I'll probably go with it. – Tex Jun 7 '11 at 11:52
@Tex you can still add your own filter or hook. functions.php always runs before any other file. – kaiser Jun 7 '11 at 11:56
"Place his code in your functions.php file." - Actually, since the OP state that he doesn't want to edit Theme files, I would recommend either 1) place the code in a Child Theme functions.php file, or 2) place the code in a site-specific Plugin. – Chip Bennett Apr 4 '12 at 16:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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