Skip to content

Instantly share code, notes, and snippets.

@ksk1015
Created December 14, 2018 19:09
Show Gist options
  • Save ksk1015/0493ce3f1b8d9bc2f722559822fa48a6 to your computer and use it in GitHub Desktop.
Save ksk1015/0493ce3f1b8d9bc2f722559822fa48a6 to your computer and use it in GitHub Desktop.
function my_enqueue_inline_script ($handle, $src = false, $deps = array(), $ver = false, $in_footer = false) {
if ( $src ) {
wp_enqueue_script( $handle, $src, $deps, false, $in_footer );
}
add_filter( 'script_loader_tag', function ( $tag, $this_handle, $src ) use ($handle) {
if ($this_handle !== $handle) {
return $tag;
}
$url = explode('?', $src)[0];
$path = str_replace(site_url(), ABSPATH, $url);
$script = file_get_contents( $path );
return "<script>// $url\n$script\n</script>\n";
}, 10, 3 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment