W3cubDocs

/WordPress

WP_Scripts::get_eligible_loading_strategy( string $handle ): string

Gets the best eligible loading strategy for a script.

Parameters

$handlestringrequired
The script handle.

Return

string The best eligible loading strategy.

Source

private function get_eligible_loading_strategy( $handle ) {
	$intended_strategy = (string) $this->get_data( $handle, 'strategy' );

	// Bail early if there is no intended strategy.
	if ( ! $intended_strategy ) {
		return '';
	}

	/*
	 * If the intended strategy is 'defer', limit the initial list of eligible
	 * strategies, since 'async' can fallback to 'defer', but not vice-versa.
	 */
	$initial_strategy = ( 'defer' === $intended_strategy ) ? array( 'defer' ) : null;

	$eligible_strategies = $this->filter_eligible_strategies( $handle, $initial_strategy );

	// Return early once we know the eligible strategy is blocking.
	if ( empty( $eligible_strategies ) ) {
		return '';
	}

	return in_array( 'async', $eligible_strategies, true ) ? 'async' : 'defer';
}

Changelog

Version Description
6.3.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_scripts/get_eligible_loading_strategy