Enqueue scripts to a specific header-<name>.php?

I’ve multiple header files (header-name.php), they are mainly being used by different custom post types.
Is there any way to enqueue script to only a specific header?

At this moment I’m targeting it with conditional tag like if ( 'MYPOSTTYPE' == get_post_type() ) but I’m looking for a better alternative.

Read More

do we have something like is_header('name')?

Related posts

Leave a Reply

2 comments

  1. You load the header file probably with get_header(). There is a hook you can use: 'get_header'. You get the called header name as parameter.

    add_action( 'get_header', 'wpse_54865_conditional_enqueue' );
    
    function wpse_54865_conditional_enqueue( $name )
    {
        if ( 'my_custom_header_name' === $name )
        {
            // register your script loading function
        }
    }
    
  2. The wp_enqueue_scripts action is part of the wp_head hook, so the enqueue process isn’t really tied to header.php. It’s also the appropriate way to enqueue scripts and styles. You could possibly switch to the is_singlular ( 'MYPOSTTYPE' ) conditional tag, but I’m not sure that’s really better.

    You could set a global variable before the wp_head() function in your header.php’s and then test for that variable to decide which scripts to enqueue.