Last active
May 24, 2022 00:51
-
-
Save sorindorobantu/d095e49519daa143fef67a4a53465f16 to your computer and use it in GitHub Desktop.
Wordpress Enqueue AlpineJS form CDN with IE11 support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wordpress Enqueue AlpineJS form CDN with IE11 support | |
// <script type="module" src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"></script> | |
// <script nomodule src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine-ie11.min.js" defer></script> | |
function advertis_enqueue_scripts() | |
{ | |
wp_register_script( 'AlpineJS-module', 'https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.3.5/dist/alpine.min.js', null, null, false ); | |
wp_enqueue_script('AlpineJS-module'); | |
wp_register_script( 'AlpineJS-nomodule', 'https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.3.5/dist/alpine.min.js', null, null, false ); | |
wp_enqueue_script('AlpineJS-nomodule'); | |
} | |
add_action('wp_enqueue_scripts', 'advertis_enqueue_scripts'); | |
add_filter('script_loader_tag', 'advertis_defer_script', 10, 2); | |
function advertis_defer_script($tag, $handle) | |
{ | |
if ($handle === 'AlpineJS-module') { | |
$tag = str_replace("type='text/javascript'", "type='module'", $tag); | |
} | |
if ($handle === 'AlpineJS-nomodule') { | |
$tag = str_replace("type='text/javascript'", "nomodule", $tag); | |
$tag = str_replace("type='text/javascript'", "", $tag); | |
$tag = str_replace("alpine.min.js'", "alpine.min.js' defer", $tag); | |
} | |
return $tag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script. I was having a bit of trouble trying to load AlpineJS and was starting to give up on using it. But with this I can get it loading perfectly, thank you! 👍