WordPress add script with custom type (x-jquery-tmpl)

Printing the script in the head is a method called do_item in the wp_script class. In this method you see this line:

$tag = "{$cond_before}{$before_handle}<script type="text/javascript" src="https://wordpress.stackexchange.com/questions/262406/$src"></script>\n{$after_handle}{$cond_after}";

So, the script type is hardwired in the function. However, a few lines further on, you see this:

$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );

This allows you to build a filter to change the script type. First you would enqueue your script file in the usual way:

wp_enqueue_script ('my-script-handle', 'http://.../my-script.js');

Then you would define a filter that changes the script type for this specific script:

add_filter ('script_loader_tag', wpse262406_change_script_type, 10, 3);
function wpse262406_change_script_type ($tag, $handle, $src) {
  if ($handle == 'my-script-handle')
    $tag = str_replace ('text/javascript', 'text/x-jquery-tmpl', $tag);
  return $tag;
  }

Note: I didn’t test this code, so some debugging might be necessary.

tech