├── .gitignore ├── autoload.php ├── composer.json ├── CHANGELOG.md ├── lib ├── filters.php └── functions.php ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /autoload.php: -------------------------------------------------------------------------------- 1 | =7.0.0" 8 | }, 9 | "autoload": { 10 | "files": [ 11 | "autoload.php" 12 | ] 13 | }, 14 | "authors": [ 15 | { 16 | "name": "Lukas Gaechter", 17 | "email": "lukas.gaechter@mind.ch", 18 | "homepage": "https://www.mind.ch" 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Script Loader Tags 2 | 3 | ## 1.2.1 - 2023-02-09 4 | 5 | - Fixed a bug when multiple calls to `update_script_tag()` could cause attributes to be added multiple times. 6 | 7 | ## 1.2.0 - 2020-01-15 8 | 9 | - Added possibility for `nomodule` and `type="module"` attributes. 10 | - Updated usage to use pipes instead of `--suffix`. Use `|async` instead of `--async` and `|defer` instead of `--defer`. The old suffixes are still supported. 11 | - Added new function `update_script_tag()` that can be used for all cases. The `script_loader_tag_method()` can still be used, but `update_script_tag()` is recommended. 12 | - Updated README. 13 | 14 | ## 1.1.0 - 2018-05-24 15 | 16 | - Added function `script_loader_tag_method()` to change how an already enqueued script is loaded. 17 | -------------------------------------------------------------------------------- /lib/filters.php: -------------------------------------------------------------------------------- 1 | 'async', 14 | '|defer' => 'defer', 15 | '|module' => 'type="module"', 16 | '|nomodule' => 'nomodule', 17 | // Deprecated. 18 | '--async' => 'async', 19 | '--defer' => 'async', 20 | ]; 21 | 22 | foreach ( $pipes as $pipe => $value ) { 23 | if ( strpos( $handle, $pipe ) > -1 ) { 24 | $tag = str_replace( $pipe, '', $tag ); 25 | $tag = str_replace( ' src', " {$value} src", $tag ); 26 | } 27 | } 28 | 29 | return $tag; 30 | }, 10, 3 ); 31 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 MIND Kommunikation GmbH 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/functions.php: -------------------------------------------------------------------------------- 1 | $attribute ) { 48 | if ( preg_match( "/\s{$attribute}[\s>]/", $tag ) ) { 49 | unset( $attributes[ $key ] ); 50 | } 51 | } 52 | 53 | if ( empty( $attributes ) ) { 54 | return $tag; 55 | } 56 | 57 | return str_replace( ' src', sprintf( ' %s src', join( ' ', $attributes ) ), $tag ); 58 | }; 59 | 60 | add_filter( 'script_loader_tag', $filter, 10, 2 ); 61 | } 62 | 63 | /** 64 | * Loads a script tag with the defer or async attribute. 65 | * 66 | * You don’t have to use this if your script is added in the footer. 67 | * 68 | * @param string|array $handle The handle or array of handles to apply the changes for. 69 | * @param string $method The method to use. Should be `defer` or `async`. Default `defer`. 70 | */ 71 | function script_loader_tag_method( $handle = [], $method = 'defer' ) { 72 | update_script_tag( $handle, $method ); 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Script Loader Tags 2 | 3 | Helper functionality for enqueueing scripts in WordPress themes with the following ` 48 | ``` 49 | 50 | It’s also possible to chain more than one suffix. 51 | 52 | ```php 53 | add_action( 'wp_enqueue_scripts', function() { 54 | wp_enqueue_script( 55 | 'js-app|module|async', 56 | get_theme_file_uri( 'build/js/app.js' ) 57 | ); 58 | } ); 59 | ``` 60 | 61 | ### Use a function 62 | 63 | You can use the `update_script_tag()` function to set a filter that adds the attributes. 64 | 65 | ```php 66 | add_action( 'wp_enqueue_scripts', function() { 67 | // Load Picturefill asynchronously. 68 | wp_enqueue_script( 69 | 'js-picturefill', 70 | get_theme_file_uri( 'build/js/picturefill.js' ) 71 | ); 72 | 73 | update_script_tag( 'js-picturefill', 'async' ); 74 | } ); 75 | ``` 76 | 77 | It‘s also possible to add more than one handle and/or attribute if you pass an array: 78 | 79 | ```php 80 | // Multiple attributes. 81 | update_script_tag( 'js-app', [ 'async', 'module' ] ); 82 | 83 | // Multiple handles. 84 | update_script_tag( [ 'js-frontpage', 'js-app' ], 'async' ); 85 | ``` 86 | 87 | This function is useful 88 | 89 | - if you want to change the method of an existing script that you didn’t enqueue yourself. 90 | - if you use `wp_register_script()`, where you shouldn’t add suffixes to the script handle. 91 | 92 | 93 | 94 | ## Support 95 | 96 | This is a library that we use at MIND to develop WordPress themes. You’re free to use it, but currently, we don’t provide any support. 97 | --------------------------------------------------------------------------------