skip to Main Content

I’m using the following code to place some page-specific scripts into my header in WordPress within functions.php. It’s including the code at the beginning of the header, but it needs to be loaded last in order for the page to work as it should. Is there a way to specify in the code that I want this php file to be included at the bottom of the header?

function my_post_header_function() {
   if( is_single() ) {
      include 'header-product.php';
   }
}
add_action( 'wp_head', 'my_post_header_function' );

Note: I want it to be within the <head> </head> tags still, but placed before the closing </head> tag.

2

Answers


  1. Chosen as BEST ANSWER

    Due to the limitations of placement in functions.php I instead placed it within header.php before the closing </head> tag, as follows:

    <?php if( is_single() ) {
    include 'header-product.php';
    } 
    else {} ?>
    

  2. Try changing the priority to something later. The default is 10, so try 20:

    function my_post_header_function() {
       if( is_single() ) {
          include 'header-product.php';
       }
    }
    add_action( 'wp_head', 'my_post_header_function', 20 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search