Skip to content

Instantly share code, notes, and snippets.

@zergiocosta
Created August 7, 2020 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zergiocosta/6ee53d9fe8b818f279498f75110cad06 to your computer and use it in GitHub Desktop.
Save zergiocosta/6ee53d9fe8b818f279498f75110cad06 to your computer and use it in GitHub Desktop.
Make WordPress relative URLS
<?php
function make_paths_relative($buffer) {
$home_url = esc_url(home_url('/'));
$home_url_relative = wp_make_link_relative($home_url);
$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_paths() {
ob_start('make_paths_relative');
}
function buffer_end_relative_paths() {
@ob_end_flush();
}
add_action('registered_taxonomy', 'buffer_start_relative_paths');
add_action('shutdown', 'buffer_end_relative_paths');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment