Better rewrite/clean URL handling
- New roots-utils.php file for base functions - Defined useful constants for dealing with common paths - Refactored rewrites and clean URLs to be more flexible for non standard setups - Custom scripts handler now deals with plugin scripts correctly - Fixes #204, #270, #278
This commit is contained in:
@@ -23,30 +23,22 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
|
||||
|
||||
function roots_add_rewrites($content) {
|
||||
global $wp_rewrite;
|
||||
$theme_name = next(explode('/themes/', get_stylesheet_directory()));
|
||||
$roots_new_non_wp_rules = array(
|
||||
'css/(.*)' => 'wp-content/themes/'. $theme_name . '/css/$1',
|
||||
'js/(.*)' => 'wp-content/themes/'. $theme_name . '/js/$1',
|
||||
'img/(.*)' => 'wp-content/themes/'. $theme_name . '/img/$1',
|
||||
'plugins/(.*)' => 'wp-content/plugins/$1'
|
||||
'css/(.*)' => THEME_PATH . '/css/$1',
|
||||
'js/(.*)' => THEME_PATH . '/js/$1',
|
||||
'img/(.*)' => THEME_PATH . '/img/$1',
|
||||
'plugins/(.*)' => RELATIVE_PLUGIN_PATH . '/$1'
|
||||
);
|
||||
$wp_rewrite->non_wp_rules = $roots_new_non_wp_rules;
|
||||
return $content;
|
||||
}
|
||||
|
||||
function roots_clean_assets($content) {
|
||||
$theme_name = next(explode('/themes/', $content));
|
||||
$current_path = '/wp-content/themes/' . $theme_name;
|
||||
$new_path = '';
|
||||
$content = str_replace($current_path, $new_path, $content);
|
||||
return $content;
|
||||
}
|
||||
|
||||
function roots_clean_plugins($content) {
|
||||
$current_path = '/wp-content/plugins';
|
||||
$new_path = '/plugins';
|
||||
$content = str_replace($current_path, $new_path, $content);
|
||||
return $content;
|
||||
function roots_clean_urls($content) {
|
||||
if (strpos($content, FULL_RELATIVE_PLUGIN_PATH) === 0) {
|
||||
return str_replace(FULL_RELATIVE_PLUGIN_PATH, WP_BASE . '/plugins', $content);
|
||||
} else {
|
||||
return str_replace('/' . THEME_PATH, '', $content);
|
||||
}
|
||||
}
|
||||
|
||||
// only use clean urls if the theme isn't a child or an MU (Network) install
|
||||
@@ -54,12 +46,16 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
|
||||
add_action('generate_rewrite_rules', 'roots_add_rewrites');
|
||||
add_action('generate_rewrite_rules', 'roots_add_h5bp_htaccess');
|
||||
if (!is_admin()) {
|
||||
add_filter('plugins_url', 'roots_clean_plugins');
|
||||
add_filter('bloginfo', 'roots_clean_assets');
|
||||
add_filter('stylesheet_directory_uri', 'roots_clean_assets');
|
||||
add_filter('template_directory_uri', 'roots_clean_assets');
|
||||
add_filter('script_loader_src', 'roots_clean_plugins');
|
||||
add_filter('style_loader_src', 'roots_clean_plugins');
|
||||
$tags = array(
|
||||
'plugins_url',
|
||||
'bloginfo',
|
||||
'stylesheet_directory_uri',
|
||||
'template_directory_uri',
|
||||
'script_loader_src',
|
||||
'style_loader_src'
|
||||
);
|
||||
|
||||
add_filters($tags, 'roots_clean_urls');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,4 +81,4 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user