How can I remove the site URL from enqueued scripts and styles?

I’m dealing with an SSL issue and I would like to strip the domain from all scripts and styles being output via wp_enqueue_scripts. This would result in all scripts and styles being displayed with a relative path from the domain root.

I imagine there is a hook that I can use to fileter this, however, I am not sure which one, nor how to go about it.

Related posts

Leave a Reply

3 comments

  1. Similar to Wyck’s answer, but using str_replace instead of regex.

    script_loader_src and style_loader_src are the hooks you want.

    <?php
    add_filter( 'script_loader_src', 'wpse47206_src' );
    add_filter( 'style_loader_src', 'wpse47206_src' );
    function wpse47206_src( $url )
    {
        if( is_admin() ) return $url;
        return str_replace( site_url(), '', $url );
    }
    

    You could also start the script/style URLs with a double slash // (a “network path reference“). Which might be safer (?): still has the full path, but uses the scheme/protocol of the current page.

    <?php
    add_filter( 'script_loader_src', 'wpse47206_src' );
    add_filter( 'style_loader_src', 'wpse47206_src' );
    function wpse47206_src( $url )
    {
        if( is_admin() ) return $url;
        // why pass by reference on count? last arg
        return str_replace( array( 'http:', 'https:' ), '', $url, $c=1 );
    }
    
  2. Yes, i think its possible. See on the filter hook script_loader_src; there get the string and you can filter this for your requirements.

    add_filter( 'script_loader_src', 'fb_filter_script_loader', 1 );
    function fb_filter_script_loader( $src ) {
    
        // remove string-part "?ver="
        $src = explode( '?ver=', $src );
    
        return $src[0];
    }
    
    • write on scratch, not tested

    The same is possible for stylesheets, ther load via wp_enqueue_style with filterstyle_loader_src.

  3. Another way, which I think I got from the roots theme, maybe a bit ghetto but has some smart handling on when to use relative urls (tested only on dev site). The benefit is it can be uses as a filter on many other built in urls that WordPress uses. This example only shows style and script enqueue filter.

    function roots_root_relative_url($input) {
      $output = preg_replace_callback(
        '!(https?://[^/|"]+)([^"]+)?!',
        create_function(
          '$matches',
          // if full URL is site_url, return a slash for relative root
          'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' .
          // if domain is equal to site_url, then make URL relative
          '} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' .
          // if domain is not equal to site_url, do not make external link relative
          '} else { return $matches[0]; };'
        ),
        $input
      );
    
      /**
       * Fixes an issue when the following is the case:
       * site_url() = http://yoursite.com/inc
       * home_url() = http://yoursite.com
       * WP_CONTENT_DIR = http://yoursite.com/content
       * http://codex.wordpress.org/Editing_wp-config.php#Moving_wp-content
       */
      $str = "/" . end(explode("/", content_url()));
      if (strpos($output, $str) !== false) {
        $arrResults = explode( $str, $output );
        $output = $str . $arrResults[1];
      }
    
      return $output;
    
    if (!is_admin()) {
      add_filter('script_loader_src', 'roots_root_relative_url');
      add_filter('style_loader_src', 'roots_root_relative_url');
     }