Targeting Template Pages Wordpress FSE

I have more of a coding question. It maybe a question for stack overflow. I usually load custom scripts via a function (in functions.php)
I need to load a couple of custom scripts for only 2 specific pages to avoid it loading on all pages where it is irrelevant. I have for years done so with the function is_page with an array pages I need it loaded on targeting with the page slug. Example below;

// GOOGLE MAPS INTEGRATION
function mossco_map_script() {
  if(is_page( array('home', 'contact'))){ 
      wp_register_script( 'google-custom-script', get_stylesheet_directory_uri() . '/assets/js/maps-ardee.js','','',true);

      //LOAD SCRIPTS
      wp_enqueue_script( 'google-custom-script' );
  }
}
add_action( 'wp_enqueue_scripts', 'mossco_map_script' );

My question is with Wordpress FSE and the use of Templates instead of Pages what function would I use? is_page_template ? I’ve tried that but custom Templates how would I target them? No slug or ID that I can find. I also notice that the templates folder does not store any custom Templates. I have tried targeting /templates/home.html. Wordpress documentation, it says that is_page_template () compares against a “template name”, if one is provided.

I don’t know if any of the advanced users have attempted this.

Hi @hopscotch,

This is a good point.

When I last checked this out (as FSE is continuously being refined), there weren’t any direct functions to tell you what template was being applied to a page/post.

We use the following filters to read the current template applied/or add our own (depending on the conditions set):

But the information returned by those is not 100% correct and rather convoluted.


Sounds like we could provide a simple filter with the slug of the applied template as I can see this being useful in a lot of cases.

For template ids, you’ll want to check the URL parameter postId when accessing it in the Site Editor… At least that’s the simplest way!

1 Like

The slug filter would be very beneficial. In the meantime I will mess about with those functions that you provided. Many thanks!

Hello @hopscotch,

Just to let you know that you should now be able to include scripts targeting specific templates in 1.2.9.5.8.3.
More specifically, the cwicly/themer/templates action → Actions - Documentation

If you have any questions or feedback, please don’t hesitate.

Cheers,

2 Likes

Oh lovely many thanks @Louis for such a quick turn around on this solution😊

1 Like