Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use get_header action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
December 25, 2022
5 minutes read

Hey there! If you’re diving deep into WordPress development, you’ll definitely come across various hooks that make life easier. Today, let’s chat about the get_header action. This hook fires before the header template file is loaded. To use it, you need to register it using add_action. You can place this code in your theme’s functions.php file or, even better, in a custom WordPress plugin to ensure nothing breaks when you update your theme.

Here’s how you can work with the get_header action:

Parameters

Below are the 2 parameters required for this hook:

  • $name: (string|null) Name of the specific header file to use. Null for the default header.
  • $args: (array) Additional arguments passed to the header template.

Live Example 1: Conditional Header Styles

Sometimes, you might want to enqueue different styles depending on the header being used. Here’s a straightforward example:

	function weplugins_themeslug_header_hook( $name ) {
		if ( 'new' == $name ) {
			add_action( 'wp_enqueue_scripts', 'weplugins_themeslug_header_style' );
		}
	}
	add_action( 'get_header', 'weplugins_themeslug_header_hook' );
	
	function weplugins_themeslug_header_style() {
		wp_enqueue_style( 'weplugins-header-new-style', get_template_directory_uri() . '/css/header-new.css' );
	}
	

Live Example 2: Custom Function on Header Load

Here’s an example of how you can use this hook to execute a custom function when the header is loaded:

	function weplugins_execute_on_get_header_event($name, $args){
		// Custom functionality here
	}
	// Add the action
	add_action( "get_header", "weplugins_execute_on_get_header_event" , 10, 2);
	

Live Example 3: Removing a Hook Callback

Need to remove a registered hook? Here’s how you can do it:

	remove_action( "get_header", "weplugins_execute_on_get_header_event", 10, 2 );
	

Ensure you provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook or need customization, please contact us. We’d be happy to assist you!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.