Created
October 2, 2022 08:20
-
-
Save wpflames/eb06e01f8143ba238aa3458b1acb5a32 to your computer and use it in GitHub Desktop.
Enqueue Scripts Asynchronously
This file contains hidden or 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
| <?php defined( 'ABSPATH' ) || exit; | |
| // ========================================================================= | |
| // ASYNC LOAD | |
| // ========================================================================= | |
| function my_async_scripts($url){ | |
| if ( strpos( $url, '#asyncload') === false ) | |
| return $url; | |
| else if ( is_admin() ) | |
| return str_replace( '#asyncload', '', $url ); | |
| else | |
| return str_replace( '#asyncload', '', $url )."' async='true"; | |
| } | |
| add_filter( 'clean_url', 'my_async_scripts', 11, 1 ); |
This file contains hidden or 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
| <?php | |
| // ============================================================= | |
| // Enqueue Scripts Asynchronously | |
| // ============================================================= | |
| function add_custom_scripts() { | |
| wp_enqueue_script('javascript', THEME .('/dist/index.js#asyncload'), array('jquery'), '1.0', true); | |
| } | |
| add_action( 'wp_enqueue_scripts', 'add_custom_scripts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment