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

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

Leave a Reply

Your email address will not be published. Required fields are marked *