WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/get header

Description

get_header is a hook that gets run at the very start of the get_header function call. If you pass in the name for a specific header file, like get_header( 'new' ), the do_action will pass in the name as a parameter for the hook. This allows you to limit your add_action calls to specific templates if you wish. Actions added to this hook should be added to your functions.php file.

Note: This hook is best to use to set up and execute code that doesn't get echoed to the browser until later in the page load. Anything you echo will show up before any of the markup is displayed.

Example

The following example will enqueue stylesheets conditionally for a different headers. This is just one example of how you may use the hook, and will use a secondary template file of header-new.php

function themeslug_header_hook( $name ) {
	if ( 'new' == $name ) {
		add_action( 'wp_enqueue_scripts', 'themeslug_header_style' );
	}
}
add_action( 'get_header', 'themeslug_header_hook' );

function themeslug_header_style(){
	wp_enqueue_style( 'header-new-style', get_template_directory_uri() . '/css/header-new.css' );
}

See also