-
-
Save cdsalmons/c7f0f9ef5e68506a374d to your computer and use it in GitHub Desktop.
Relative URLs in WordPress (hide 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
<?php | |
function roots_root_relative_url($input) { | |
$output = preg_replace_callback( | |
'!(https?://[^/|"]+)([^"]+)?!', | |
create_function( | |
'$matches', | |
// if full URL is site_url, return a slash for relative root | |
'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' . | |
// if domain is equal to site_url, then make URL relative | |
'} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' . | |
// if domain is not equal to site_url, do not make external link relative | |
'} else { return $matches[0]; };' | |
), | |
$input | |
); | |
return $output; | |
} | |
// workaround to remove the duplicate subfolder in the src of JS/CSS tags | |
// example: /subfolder/subfolder/css/style.css | |
function roots_fix_duplicate_subfolder_urls($input) { | |
$output = roots_root_relative_url($input); | |
preg_match_all('!([^/]+)/([^/]+)!', $output, $matches); | |
if (isset($matches[1]) && isset($matches[2])) { | |
if ($matches[1][0] === $matches[2][0]) { | |
$output = substr($output, strlen($matches[1][0]) + 1); | |
} | |
} | |
return $output; | |
} | |
if (!is_admin() && !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) { | |
add_filter('bloginfo_url', 'roots_root_relative_url'); | |
add_filter('theme_root_uri', 'roots_root_relative_url'); | |
add_filter('stylesheet_directory_uri', 'roots_root_relative_url'); | |
add_filter('template_directory_uri', 'roots_root_relative_url'); | |
add_filter('script_loader_src', 'roots_fix_duplicate_subfolder_urls'); | |
add_filter('style_loader_src', 'roots_fix_duplicate_subfolder_urls'); | |
add_filter('plugins_url', 'roots_root_relative_url'); | |
add_filter('the_permalink', 'roots_root_relative_url'); | |
add_filter('wp_list_pages', 'roots_root_relative_url'); | |
add_filter('wp_list_categories', 'roots_root_relative_url'); | |
add_filter('wp_nav_menu', 'roots_root_relative_url'); | |
add_filter('the_content_more_link', 'roots_root_relative_url'); | |
add_filter('the_tags', 'roots_root_relative_url'); | |
add_filter('get_pagenum_link', 'roots_root_relative_url'); | |
add_filter('get_comment_link', 'roots_root_relative_url'); | |
add_filter('month_link', 'roots_root_relative_url'); | |
add_filter('day_link', 'roots_root_relative_url'); | |
add_filter('year_link', 'roots_root_relative_url'); | |
add_filter('tag_link', 'roots_root_relative_url'); | |
add_filter('the_author_posts_link', 'roots_root_relative_url'); | |
} | |
// remove root relative URLs on any attachments in the feed | |
function roots_root_relative_attachment_urls() { | |
$roots_options = roots_get_theme_options(); | |
if (!is_feed() && $roots_options['root_relative_urls']) { | |
add_filter('wp_get_attachment_url', 'roots_root_relative_url'); | |
add_filter('wp_get_attachment_link', 'roots_root_relative_url'); | |
} | |
} | |
add_action('pre_get_posts', 'roots_root_relative_attachment_urls'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment