wp_script_add_data( string $handle, string $key, mixed $value ): bool

Adds metadata to a script.

Description

Works only if the script has already been registered.

Possible values for $key and $value: ‘conditional’ string Comments for IE 6, lte IE 7, etc.

See also

Parameters

$handlestringrequired
Name of the script.
$keystringrequired
Name of data point for which we’re storing a value.
$valuemixedrequired
String containing the data to be added.

Return

bool True on success, false on failure.

Source

function wp_script_add_data( $handle, $key, $value ) {
	return wp_scripts()->add_data( $handle, $key, $value );
}

Changelog

VersionDescription
4.2.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Enqueue IE-specific Scripts with conditional comments

    function wpdemo_enqueue_scripts() {
        wp_enqueue_script( 'wpdemo-respond', get_template_directory_uri().'/js/respond.min.js' );
        wp_script_add_data( 'wpdemo-respond', 'conditional', 'lt IE 9' );
    
        wp_enqueue_script( 'wpdemo-html5shiv',get_template_directory_uri().'/js/html5shiv.js');
        wp_script_add_data( 'wpdemo-html5shiv', 'conditional', 'lt IE 9' );
    }
    add_action( 'wp_enqueue_scripts', 'wpdemo_enqueue_scripts' );
  2. Skip to note 4 content

    Add IE-specific CDN Scripts with conditional comments

    function add_scripts_for_IE() {
    	wp_enqueue_script( 'html5shiv.min.js', '//oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js' );
    	wp_script_add_data( 'html5shiv.min.js', 'conditional', 'lt IE 9' );
    
    	wp_enqueue_script( 'respond.min.js',  '//oss.maxcdn.com/respond/1.4.2/respond.min.js' );
    	wp_script_add_data( 'respond.min.js', 'conditional', 'lt IE 9' );
    }
    add_action( 'wp_enqueue_scripts', 'add_scripts_for_IE' );

You must log in before being able to contribute a note or feedback.