Created
January 17, 2019 09:42
-
-
Save icetee/706f72f835025ab56807884f63ab68f7 to your computer and use it in GitHub Desktop.
Use relative URL for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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