• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress a2z

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / TwentyTwenty_Script_Loader::filter_script_loader_tag() – Adds async/defer attributes to enqueued / registered scripts.

You appear to be a bot. Output may be restricted

Description

Adds async/defer attributes to enqueued / registered scripts.

If #12009 lands in WordPress, this function can no-op since it would be handled in core.

Usage

$string = TwentyTwenty_Script_Loader::filter_script_loader_tag( $tag, $handle );

Parameters

$tag
( string ) required – The script tag.
$handle
( string ) required – The script handle.

Returns

string Script HTML string.

Source

File name: twentytwenty/classes/class-twentytwenty-script-loader.php


Lines:

1 to 15 of 15
    public function filter_script_loader_tag( $tag, $handle ) {
      foreach ( array( 'async', 'defer' ) as $attr ) {
        if ( ! wp_scripts()->get_data( $handle, $attr ) ) {
          continue;
        }
        // Prevent adding attribute when already added in #12009.
        if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) {
          $tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 );
        }
        // Only allow async or defer, not both.
        break;
      }
      return $tag;
    }
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 8th April 2020

Primary Sidebar

Information

Function name: TwentyTwenty_Script_Loader::filter_script_loader_tag
Class ref: TwentyTwenty_Script_Loader
Plugin ref: Twenty Twenty
Version: 1.6
Sourcefile: classes/class-twentytwenty-script-loader.php
File ref: classes/class-twentytwenty-script-loader.php
Deprecated?: No
API Letters: F,L,S,T

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 5.6. PHP: 7.3.26
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  wp-a2z.org
© Copyright WordPress a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites