Advertisement
sierre

Preload External Scripts and Prefetch DNS

Jun 27th, 2025
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. /*== Preload External Scripts ==*/
  2. add_action('wp_head', function () {
  3.     // Define your script URLs
  4.     $scripts = [
  5.         "https://sw-assets.ekomiapps.de/static_resources/slick.js",
  6.         "https://maps.googleapis.com/maps-api-v3/api/js/61/6d/controls.js",
  7.         "https://maps.googleapis.com/maps-api-v3/api/js/61/6d/places_impl.js",
  8.         "https://widget.intercom.io/widget/livuebsx",
  9.         "https://js.intercomcdn.com/frame-modern.a96890ff.js",
  10.         "https://js.intercomcdn.com/vendor-modern.2adcf83e.js"
  11.     ];
  12.  
  13.     // Output <link rel="preload"> tags
  14.     foreach ($scripts as $src) {
  15.         echo '<link rel="preload" as="script" href="' . esc_url($src) . '">' . "\n";
  16.     }
  17.  
  18.     // Output <script> tags with async/defer based on type
  19.     ?>
  20.     <script>
  21.         (function() {
  22.             const scripts = [
  23.                 { src: "https://sw-assets.ekomiapps.de/static_resources/slick.js", async: false },
  24.                 { src: "https://maps.googleapis.com/maps-api-v3/api/js/61/6d/controls.js", async: false },
  25.                 { src: "https://maps.googleapis.com/maps-api-v3/api/js/61/6d/places_impl.js", async: false },
  26.                 { src: "https://widget.intercom.io/widget/livuebsx", async: true },
  27.                 { src: "https://js.intercomcdn.com/frame-modern.a96890ff.js", async: false },
  28.                 { src: "https://js.intercomcdn.com/vendor-modern.2adcf83e.js", async: false }
  29.             ];
  30.  
  31.             scripts.forEach(({ src, async }) => {
  32.                 const script = document.createElement("script");
  33.                 script.src = src;
  34.                 script.async = async;
  35.                 script.defer = !async;
  36.                 document.head.appendChild(script);
  37.             });
  38.         })();
  39.     </script>
  40.     <?php
  41. }, 1); // Priority 1
  42.  
  43. /*== END - Preload External Scripts ==*/
  44.  
  45. /*== Prefetch Transport.io Quote Form from its system ==*/
  46. add_action('wp_head', function () {
  47.     echo '<link rel="dns-prefetch" href="//booking.buscharter.com.au">' . "\n";
  48.     echo '<link rel="preconnect" href="https://booking.buscharter.com.au" crossorigin>' . "\n";
  49. }, 1);
  50. /*== END - Prefetch Transport.io Quote Form from its system ==*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement