0

I would like to a add the following script into WordPress:

<script type="text/x-jquery-tmpl">  
  ...
</script>

but using:

wp_add_inline_script( 'my-script', '...' );

will give me:

<script type="text/javascript"> 
  ...
</script>

Must I edit header.php for any type of script that is not javascript?

2

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='$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.

  • yea, it was not working when your preg_replace() was not assigned to anything. – contemplator Apr 4 '17 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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