Support » Plugin: Statically » Optimized URL

  • Previously, I use free cdn from optimole, all urls work fine including optimized urls.

    Then use statically.
    But, statically does not replace optimized url with cdn url.

    I’ve tested it with 3 plugins:
    Autoptimize
    WP-Optimize
    LiteSpeed Cache

    So, I add code in function.php or with Code Snippets plugin to fix it.

    Example for LiteSpeed Cache plugin:

    1. goto: LiteSpeed Cache -> CDN -> Use CDN Mapping -> CDN URL
    2. paste: https://cdn.statically.io/FILE_PATH/yourdomain.com/
    3. click: Save Changes
    4. goto: Appearance -> Theme Editor -> functions.php
    5. paste this code
      // Modify output LiteSpeed CSS & JS URL after applying CDN rules
      add_filter( 'litespeed_buffer_after', function( $content ) {
        function replaceCssJs( $type, $tag, $data ) {
          $pattern = '/(<'. $tag .'[^>]+)file_path(\/[^\/]+\/[^\/]+\/litespeed\/(css|js|cssjs)\/[^\.]+\.('. $type .')[^>]+>)/i';
          if ( preg_match($pattern, $data) ) {
            if ($type == 'css') {
              // css injected above the closing </head> tag
              preg_match($pattern, $data, $css);
              $data = preg_replace($pattern, '', $data);
              $data = str_replace('</head>', $css[0] .'</head>', $data);
            }
            $data = preg_replace($pattern, '$1$3$2', $data);
          }
          return $data;
        }
        $content = replaceCssJs('css', 'link', $content);
        $content = replaceCssJs('js', 'script', $content);
        return $content;
      } );
    6. click: Update File
    • This topic was modified 1 month, 1 week ago by Yui.
    • This topic was modified 1 month, 1 week ago by sekedus.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.