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.

3 Answers 3


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

script_loader_src and style_loader_src are the hooks you want.

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.

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 );
  • Excellent, just the hooks I was looking for.
    – Ben
    Commented Mar 30, 2012 at 9:58
  • Any particular reason that you're excluding the admin section here?
    – El Yobo
    Commented Aug 5, 2014 at 7:07
  • @ElYobo Probably because you don't want to unexpectedly change HTML content you're about to edit and save. Also note you can use wp-cli to do find and replace in the database as in: wp search-replace 'http://mydomain.tld' 'https://mydomain.tld'
    – surfbuds
    Commented Mar 12, 2018 at 2:53
  • @surfbuds The issue is not related to content, it's related to the scripts/styles loaded in code. It won't affect content that you're about to edit and save and doing a search and replace in the database will likewise not solve the problem.
    – El Yobo
    Commented Mar 13, 2018 at 5:22

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.


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(
      // 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]; };'

   * 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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.