Selective styles and scripts inclusion

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

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino