Title: script_loader_tag
Published: December 18, 2014
Last modified: August 20, 2026

---

# apply_filters( ‘script_loader_tag’, string $tag, string $handle, string $src )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#user-contributed-notes)

Filters the HTML script tag of an enqueued script.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#parameters)󠁿

 `$tag`string

The `<script>` tag for the enqueued script.

`$handle`string

The script’s registered handle.

`$src`string

The script’s source URL.

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#source)󠁿

    ```php
    $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes_class-wp-scripts-php-2/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-scripts.php#L496)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-scripts.php#L496-L496)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Scripts::do_item()](https://developer.wordpress.org/reference/classes/wp_scripts/do_item/)`wp-includes/class-wp-scripts.php` |

Processes a script dependency.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.1.0](https://developer.wordpress.org/reference/since/4.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#comment-content-1902)
 2.    [opportus](https://profiles.wordpress.org/opportus/)  [  10 years ago  ](https://developer.wordpress.org/reference/hooks/script_loader_tag/#comment-1902)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fscript_loader_tag%2F%23comment-1902)
     Vote results for this note: 6[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fscript_loader_tag%2F%23comment-1902)
 4.  Useful when a script excecution depends on “ attributes, eg:
 5.      ```php
         add_filter( 'script_loader_tag', 'add_id_to_script', 10, 3 );
     
         function add_id_to_script( $tag, $handle, $src ) {
             if ( 'dropbox.js' === $handle ) {
                 $tag = '<script type="text/javascript" src="' . esc_url( $src ) . '" id="dropboxjs" data-app-key="MY_APP_KEY"></script>';
             }
     
             return $tag;
         }
         ```
     
 6.   * Missing { on line 4.
      * [Sam Kent](https://profiles.wordpress.org/oxocube/) [8 years ago](https://developer.wordpress.org/reference/hooks/script_loader_tag/#comment-2797)
      * Is it possible to do with Array? I have several Scripts that I want to do this,
        can I just replicate this code by changing the scripts? An Array would be better,
        right?
      * [ofmarconi](https://profiles.wordpress.org/ofmarconi/) [4 years ago](https://developer.wordpress.org/reference/hooks/script_loader_tag/#comment-5867)
      * When I check this with PHPCS against the WordPress coding standard, it returns
        and error `Scripts must be registered/enqueued via wp_enqueue_script()`
      * [Peter Hebert](https://profiles.wordpress.org/peterhebert/) [3 years ago](https://developer.wordpress.org/reference/hooks/script_loader_tag/#comment-6883)
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fscript_loader_tag%2F%3Freplytocom%3D1902%23feedback-editor-1902)
 8.   [Skip to note 4 content](https://developer.wordpress.org/reference/hooks/script_loader_tag/?output_format=md#comment-content-5507)
 9.    [Geoffrey Brossard](https://profiles.wordpress.org/geoffreybr/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/script_loader_tag/#comment-5507)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fscript_loader_tag%2F%23comment-5507)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fscript_loader_tag%2F%23comment-5507)
 11. It can be used to defer or async all JS scripts :
 12.     ```php
         function prefix_defer_js( $html ) {
             if ( ! is_admin() ) {
                 $html = str_replace( '></script>', ' defer></script>', $html );
             }
             return $html;
         }
         add_filter( 'script_loader_tag', 'prefix_defer_js' );
         ```
     
 13. You can also use the `$handle` argument to filter scripts.
 14.  * To expand on Geoffrey Brossard’s note, you might run into issues deferring 
        all, the example below you can exclude the handles you might need from this
        filter like so:
      *     ```php
               function prefix_defer_js($tag, $handle) {  	if (is_admin()) return $tag;  	 	$exclude_handles = [  	 		'heartbeat',  	 		'wp-hooks',  	 		'wp-auth-check',  	 		'jquery',  	 		'jquery-core',   			'jquery-ui-core',  	 		'wp-i18n'  	 	];  	 	if (in_array($handle, $exclude_handles)) {  	 		$tag = $tag;  	 	} else {  	 		$tag = str_replace('>', ' defer>', $tag);  	 	}   		return $tag;  	 }  	 add_filter('script_loader_tag', 'prefix_defer_js', 10, 2); 
            ```
        
      * [studiolxv](https://profiles.wordpress.org/studiolxv/) [4 years ago](https://developer.wordpress.org/reference/hooks/script_loader_tag/#comment-6257)
 15.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fscript_loader_tag%2F%3Freplytocom%3D5507%23feedback-editor-5507)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fscript_loader_tag%2F)
before being able to contribute a note or feedback.