Forked from sorindorobantu/Wordpress Enqueue AlpineJS
Created
February 16, 2021 20:20
-
-
Save design-innovations/87e742ab6392afb22928cbf8b7df5305 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