Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>For WordPress 4.2+</strong></p> <p>Use <a href="https://developer.wordpress.org/reference/functions/wp_script_add_data/" rel="noreferrer"><code>wp_script_add_data</code></a> to wrap the script in a conditional comment:</p> <pre><code>wp_enqueue_script( 'html5shiv', '//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.js' ); wp_script_add_data( 'html5shiv', 'conditional', 'lt IE 9' ); wp_enqueue_script( 'respond', get_template_directory_uri() . '/js/respond.min.js' ); wp_script_add_data( 'respond', 'conditional', 'lt IE 9' ); </code></pre> <p>Just make sure you register the script before invoking <code>wp_script_add_data</code> (registering is handled by <code>wp_enqueue_script</code> above), and that the first argument you pass to <code>wp_script_add_data</code> matches the first argument you passed when you registered the script.</p> <hr> <p><strong>For WordPress 4.1</strong></p> <p>You can use the <a href="https://developer.wordpress.org/reference/hooks/script_loader_tag/" rel="noreferrer"><code>script_loader_tag</code> filter</a> today to enqueue scripts wrapped in conditional comments. Here’s an example implementation:</p> <pre><code>wp_enqueue_script( 'html5shiv', '//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.js', array(), '3.7.2', false ); add_filter( 'script_loader_tag', function( $tag, $handle ) { if ( $handle === 'html5shiv' ) { $tag = "&lt;!--[if lt IE 9]&gt;$tag&lt;![endif]--&gt;"; } return $tag; }, 10, 2 ); </code></pre> <p>And if you want to include more than one script in the same way, you can use something like:</p> <pre><code>// Conditional polyfills $conditional_scripts = array( 'html5shiv' =&gt; '//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.js', 'html5shiv-printshiv' =&gt; '//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv-printshiv.js', 'respond' =&gt; '//cdn.jsdelivr.net/respond/1.4.2/respond.min.js' ); foreach ( $conditional_scripts as $handle =&gt; $src ) { wp_enqueue_script( $handle, $src, array(), '', false ); } add_filter( 'script_loader_tag', function( $tag, $handle ) use ( $conditional_scripts ) { if ( array_key_exists( $handle, $conditional_scripts ) ) { $tag = "&lt;!--[if lt IE 9]&gt;$tag&lt;![endif]--&gt;"; } return $tag; }, 10, 2 ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload