0

I'd like to know if is there any method that allows to include scripts on specific pages/templates depending if those page have a specific get_template_part inside.

Example: I have more than one page templates that contain a very specific get_template_part( 'form', 'search' ) included. I need the inclusion of a script (let's say jQuery UI) on all the pages that refer to a template that includes form-search.php.

I usually include scripts in wp_enqueue_scripts hook in functions.php file and sometimes I put a condition to the inclusion, like in the example below

function snake_child_scripts () {
    /*
     * Load Swiper script
     *
     */
    wp_enqueue_script( 'swiper', Snake::$stylesheet_dir_url . '/js/vendor/swiper/swiper-bundle.min.js', array(), '8.3.1' );

    /*
     * Load theme main script
     *
     */
    wp_enqueue_script( 'snake-child-script-main', Snake::$stylesheet_dir_url . '/js/script.js', array( 'jquery' ), Snakeskin_Commons::$version );

    /*
     * Home scripts
     *
     */
    if ( is_front_page() ) :
        wp_enqueue_script( 'snake-child-typewriter', Snake::$stylesheet_dir_url . '/js/typewriter.js', array(), Snakeskin_Commons::$version, true );
    endif;
}
add_action( 'wp_enqueue_scripts', 'snake_child_scripts' );

Does a condition (like is_front_page() of the example) that allows me to check if that template part is included in the template exists?

1 Answer 1

0

Example: I have more than one page templates that contain a very specific get_template_part( 'form', 'search' ) included. I need the inclusion of a script (let's say jQuery UI) on all the pages that refer to a template that includes form-search.php.

For a Block Theme

YES!

Because WP has to load the template before it displays the page this is possible, but you wouldn't want to do it this way, there is a better more efficient and foolproof method.

WP will only load a blocks scripts and styles if it appears on the page, so turning your custom search solution into a block will make WordPress automatically do this for you for free!

For a Classic all PHP theme

For classic themes it's harder and the news is not as nice, but not impossible.

Does a condition (like is_front_page() of the example) that allows me to check if that template part is included in the template exists?

No, you can't know if a template contains a template part without loading it, because it's a PHP file that is executed, not a document that can be scanned. Doing this without loading and executing the template requires time travel technology.

The literal answer to your literal question is a hard no. To do this you would need to know in advance if the template part is included, but you can't know that until it's included.

That doesn't mean your problem can't be solved, just that the method you proposed is a dead end.

You can do it for top level templates, this is because by the time WP tries to enqueue things the template has already been chosen and loaded and you're in wp_head.

For template parts, you have several options:

  • if you know in advance that a particular template will need this, you can write code to find it. Note this is the opposite of automating, aka I the developer know that I will need this on search.php so if is_search is true enqueue it.
  • enqueue it in the template itself, note that with this method it's not possible to put the enqueue in the header (unless you have the secret to time travel), the scripts will appear in the footer instead if it hasn't already been enqueued.
  • you could start an output buffer before any templates are loaded, then at the end search the buffer to see if your template part is present and insert the HTML for your script. I do not recommend this and mention it for completeness.
    • Note that doing this is catastrophic for performance and time to first byte. While PHP is putting the output in the output buffer the browser is waiting with nothing to do and no progressive rendering can occurs. Most of the things tools such as google page speed want you to do are to optimise getting the page up infront of the user as fast as possible and none of them work if you do this. Ironically this is how plugins such as autoptimise work, they're great for bumping up your google page speed score, but terrible for real-world actual performance.
    • A faster alternative to this option is giving up and always enqueuing your script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.