Skip to content

Instantly share code, notes, and snippets.

@icetee
Created January 17, 2019 09:42
Show Gist options
  • Save icetee/706f72f835025ab56807884f63ab68f7 to your computer and use it in GitHub Desktop.
Save icetee/706f72f835025ab56807884f63ab68f7 to your computer and use it in GitHub Desktop.
Use relative URL for WordPress
function callback_relative_url($buffer) {
// Replace normal URLs
$home_url = esc_url(home_url('/'));
$home_url_relative = wp_make_link_relative($home_url);
// Replace URLs in inline scripts
$home_url_escaped = str_replace('/', '\/', $home_url);
$home_url_escaped_relative = str_replace('/', '\/', $home_url_relative);
$buffer = str_replace($home_url, $home_url_relative, $buffer);
$buffer = str_replace($home_url_escaped, $home_url_escaped_relative, $buffer);
return $buffer;
}
function buffer_start_relative_url() { ob_start('callback_relative_url'); }
function buffer_end_relative_url() { @ob_end_flush(); }
// http://codex.wordpress.org/Plugin_API/Action_Reference
add_action('registered_taxonomy', 'buffer_start_relative_url');
add_action('shutdown', 'buffer_end_relative_url');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment