bherrero
10/25/2016 - 12:04 PM

Añadir atributos "async" y "defer" a los scripts en cola || Add "async" and "defer" to enqueued scripts

Añadir atributos "async" y "defer" a los scripts en cola || Add "async" and "defer" to enqueued scripts

/**
 * Añadir atributos "defer" y "async" a la carga de scripts
 */
add_filter('script_loader_tag', 'add_async_attribute', 10, 2);
add_filter('script_loader_tag', 'add_defer_attribute', 10, 2);

// Async
function add_async_attribute($tag, $handle) {
   // add script handles to the array below
   $scripts_to_async = array( 'googlemaps' );
   
   foreach($scripts_to_async as $async_script) {
      if ($async_script === $handle) {
         return str_replace(' src', ' async="async" src', $tag);
      }
   }
   return $tag;
}

// Defer
function add_defer_attribute($tag, $handle) {
   // add script handles to the array below
   $scripts_to_defer = array( 'googlemaps' );
   
   foreach($scripts_to_defer as $defer_script) {
      if ($defer_script === $handle) {
         return str_replace(' src', ' defer="defer" src', $tag);
      }
   }
   return $tag;
}