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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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 );
}

Method 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.

Method 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');
 }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x