Skip to content

Instantly share code, notes, and snippets.

@wycks
Created April 6, 2012 00:08
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save wycks/2315295 to your computer and use it in GitHub Desktop.
Save wycks/2315295 to your computer and use it in GitHub Desktop.
Rewrite static theme assets and plugins directory (WordPress)
<?php
// rewrite /wp-content/themes/theme-name/css/ to /css/
// rewrite /wp-content/themes/theme-name/js/ to /js/
// rewrite /wp-content/themes/theme-name/img/ to /img/
// rewrite /wp-content/plugins/ to /plugins/
function roots_flush_rewrites() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function roots_add_rewrites($content) {
$theme_name = next(explode('/themes/', get_stylesheet_directory()));
global $wp_rewrite;
$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'
);
$wp_rewrite->non_wp_rules += $roots_new_non_wp_rules;
}
add_action('admin_init', 'roots_flush_rewrites');
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;
}
add_action('generate_rewrite_rules', 'roots_add_rewrites');
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');
?>
@gmaggio
Copy link

gmaggio commented Oct 23, 2012

Hi,

This is interesting stuff. Thanks for sharing. However, the URL rewrite doesn't seem to work. Only the roots_clean_assets seems to be processed, thus results in broken links.

For example, if I changed the rewrite for the image folder to something like this:

'media/(.*)' => 'wp-content/themes/'. $theme_name . '/img/$1'

The resulting URL was something like this: www.example-site.com/img/file.jpg

Shouldn't it be like this: www.example-site.com/media/file.jpg

And it is actually looking for the 'img' folder in the root directory, thus the broken link.

Am I missing something?

PS: I think you missed a closing '}' at the end.

Cheers,
ALDI

@gmaggio
Copy link

gmaggio commented Oct 23, 2012

Oops... Sorry, I didn't get it before. I guess the rewrite directory is supposed to be the same as the original folder, isn't it? I wasn't supposed to change it, right? Fixed it and now it works. Sorry for the misunderstanding. Cheers!

@rusvnc
Copy link

rusvnc commented Jan 24, 2013

Thanks for the contribution. Can you include some commenting or documentation on how this is to be used and what it might break?

@roryheaney
Copy link

On twenty thirteen theme, would this go in the functions.php?

@mircobabini
Copy link

just for completion: this go in functions.php, of course, in every theme.

@Brushez
Copy link

Brushez commented Jan 22, 2017

Only variables should be passed by reference
error line
$theme_name = next(explode('/themes/', $content));

Copy link

ghost commented Jun 12, 2017

@Brushez check my fork and comment to get rid of that error.

@amit648
Copy link

amit648 commented Jan 10, 2018

$theme_name = next(explode('/themes/', $content));

to

$nval = explode( '/themes/', $content );
$theme_name = next( $nval );

$theme_name = next(explode('/themes/', get_stylesheet_directory()));

to

$mval = explode( '/themes/', get_stylesheet_directory() );
$theme_name = next( $mval );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment