Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lgg/fa794c5c50c66c996bb8ea96ae0c0d10 to your computer and use it in GitHub Desktop.
Save lgg/fa794c5c50c66c996bb8ea96ae0c0d10 to your computer and use it in GitHub Desktop.
Make WordPress to use protocol relative urls
//make other links relative
add_filter ('site_url', 'wp_make_theme_links_protocols_relative');
add_filter ('get_option_siteurl', 'wp_make_theme_links_protocols_relative');
add_filter ('stylesheet_directory_uri', 'wp_make_theme_links_protocols_relative');
add_filter ('template_directory_uri', 'wp_make_theme_links_protocols_relative');
add_filter ('wp_get_attachment_url', 'wp_make_theme_links_protocols_relative');
add_filter ('wp_get_attachment_thumb_url', 'wp_make_theme_links_protocols_relative');
add_filter ('the_permalink', 'wp_make_theme_links_protocols_relative');
function wp_make_theme_links_protocols_relative( $link ){
$link = str_replace("http://", "//", $link);
$link = str_replace("https://", "//", $link);
return $link;
}
//Make srcset internal
add_filter( 'wp_calculate_image_srcset', 'ssl_srcset' );
function ssl_srcset( $sources ) {
foreach ( $sources as &$source ) {
$link = str_replace("http://", "//", $source['url']);
$link = str_replace("https://", "//", $link);
$source['url'] = $link;
}
return $sources;
}
//Make internal links relative
add_filter( 'the_permalink', 'wp_make_link_relative' );
add_action( 'template_redirect', 'rw_relative_urls' );
function rw_relative_urls() {
// Don't do anything if:
// - In feed
// - In sitemap by WordPress SEO plugin
if ( is_feed() || get_query_var( 'sitemap' ) )
return;
$filters = array(
'post_link',
'post_type_link',
'page_link',
'attachment_link',
'get_shortlink',
'post_type_archive_link',
'get_pagenum_link',
'get_comments_pagenum_link',
'get_template_directory_url',
'term_link',
'search_link',
'day_link',
'month_link',
'year_link',
);
foreach ( $filters as $filter )
{
add_filter( $filter, 'wp_make_link_relative' );
}
}
@lgg
Copy link
Author

lgg commented Jun 7, 2016

Paste this in functions.php above add_action('wp_footer', 'add_scripts');

@photocurio
Copy link

photocurio commented Mar 29, 2019

Really nice!

One addition: to filter the URLs added by plugins:

add_filter ('plugin_dir_url', 'wp_make_theme_links_protocols_relative');
add_filter ('plugins_url', 'wp_make_theme_links_protocols_relative');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment