How To Modify the Doctype in the Header

If you want to change the doctype for your site, add a namespaced attribute to the html element, or do some other changes to the boilerplate text, then its quite easy.

The following adds a Facebook Open Social attribute to the html element.

Open up the functions.php file in your child theme and add the following code. The code should be entered at the end of the file, just before the closing ?> if there is one.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
 
remove_action( 'genesis_doctype', 'genesis_do_doctype' );
add_action( 'genesis_doctype', 'child_do_doctype' );
/**
* Include Facebook Open Social namespaces.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/modify-doctype/
*/
function child_do_doctype() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo( 'html_type' ); ?>; charset=<?php bloginfo( 'charset' ); ?>" />
<?php
}
view raw functions.php This Gist brought to you by GitHub.

Alternatively, you may want to switch to the XHTML 1.0 Strict Doctype (be aware that there may be occurrences elsewhere in Genesis which uses code valid for Transitional doctype, but invalid for Strict):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
 
remove_action( 'genesis_doctype', 'genesis_do_doctype' );
add_action( 'genesis_doctype', 'child_do_doctype' );
/**
* Switch to XHTML 1.0 Strict doctype.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/modify-doctype/
*/
function child_do_doctype() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo( 'html_type' ); ?>; charset=<?php bloginfo( 'charset' ); ?>" />
<?php
}
view raw functions.php This Gist brought to you by GitHub.

Comments

  1. many thanks for this code gary…it helped me a lot in changing my doctype. at first i was hooking the new doctype codes in the header and soon realized that there were duplicate html tags created because genesis parent theme already was generating the doctype so finally stumbled upon ur post and implemented it successfully…many thanks :-)

Trackbacks

  1. [...] this is a slightly edited version of Gary’s Code. I was finding some difficulty in using his code, because of some syntax error. I have also [...]

Speak Your Mind

*