Tribe__Assets::filter_tag_async_defer( string $tag, string $handle )
Filters the Script tags to attach Async and/or Defer based on the rules we set in our Asset class.
Contents
Parameters
- $tag
-
(string) (Required) Tag we are filtering.
- $handle
-
(string) (Required) Which is the ID/Handle of the tag we are about to print.
Return
(string) Script tag with the defer and/or async attached.
Source
File: src/Tribe/Assets.php
public function filter_tag_async_defer( $tag, $handle ) { // Only filter for own own filters. if ( ! $asset = $this->get( $handle ) ) { return $tag; } // Bail when not dealing with JS assets. if ( 'js' !== $asset->type ) { return $tag; } // When async and defer are false we bail with the tag. if ( ! $asset->defer && ! $asset->async ) { return $tag; } $tag_has_async = false !== strpos( $tag, ' async ' ); $tag_has_defer = false !== strpos( $tag, ' defer ' ); $replacement = '<script '; if ( $asset->async && ! $tag_has_async ) { $replacement .= 'async '; } if ( $asset->defer && ! $tag_has_defer ) { $replacement .= 'defer '; } $replacement_src = $replacement . 'src='; $replacement_type = $replacement . 'type='; return str_replace( [ '<script src=', '<script type=' ], [ $replacement_src, $replacement_type ], $tag ); }
Changelog
Version | Description |
---|---|
4.13.0 | Introduced. |