Clean urls with Child Theme
This issue is an expansion on #18 The current child theme documentation states:
Using a child theme would allow you to keep Roots intact and up to date, while customizing it. There is a tradeoff however. One of the philosophies behind Roots is to be clean and simple which is why we rewrite URLs in the form of /wp-content/themes/roots/ to /css, /js, etc. Unfortunately, this doesn't work with child themes.
I'd like to use Roots in future projects, but I really need child themes, as I need to style the theme and I'd like to keep in sync with the latest version on git. However, a main requirement is also to have clean urls. All the plugins that I've built have them, so why can't Roots do this with a child theme?
I'd like to request that the clean urls are compatible with Roots child themes :)
so if the parent theme rewrites:
/wp-content/themes/themename/css/ -> /css/
/wp-content/themes/themename/js/ -> /js/
/wp-content/themes/themename/img/ -> /img/
what would the child theme rewrite to?
/wp-content/themes/child/css/ -> /????/css/
/wp-content/themes/child/js/ -> /????/js/
/wp-content/themes/child/img/ -> /????/img/
Personally, I would keep it hierarchical, by rewriting to something like this:
/wp-content/themes/child/css/ -> /css/<child>/
/wp-content/themes/child/js/ -> /js/<child>/
/wp-content/themes/child/img/ -> /img/<child>/
The urls that you would get would look like this for example:
/css/alpaca/style.css
/js/alpaca/slider.js
/img/alpaca/background.png
What do you think?
It seems the default for the active theme, whether it's parent or child should be standard:
/wp-content/themes/themename/css/ -> /css/
/wp-content/themes/themename/js/ -> /js/
/wp-content/themes/themename/img/ -> /img/
whether themename is parent folder or child folder.
In the case that it's a child theme, could there be .htaccess code that rewrites the parent files to look like this:
/wp-content/themes/parent/css/ -> parent/css/
/wp-content/themes/parent/js/ -> parent/js/
/wp-content/themes/parent/img/ -> parent/img/
I did this in a rather ugly manual fashion for my site by making changes to the relevant inc files. It would be a great feature to add out of the box.
My rewrites kept the default folder structure by adding exceptions to each rule. So all css files would rewrite to the roots css folder, except style.css, which is rewritten to the child. All js rewrites to the roots js folder except files prefixed with jqplugin which rewrite to the child theme. With the images, it only rewrites glyphicons to the roots img folder and everything else to the child img folder.
An extract from the .htaccess file is below.
RewriteRule ^css/style.css /wp-content/themes/child/style.css [QSA,L]
RewriteRule ^css/(.*) /wp-content/themes/roots/css/$1 [QSA,L]
RewriteRule ^js/jqplugin(.*) /wp-content/themes/child/js/jqplugin$1 [QSA,L]
RewriteRule ^js/(.*) /wp-content/themes/roots/js/$1 [QSA,L]
RewriteRule ^img/glyphicons(.*) /wp-content/themes/roots/img/glyphicons$1 [QSA,L]
RewriteRule ^img/(.*) /wp-content/themes/child/img/$1 [QSA,L]
Obviously there are limitations to this method as you need to be careful of the file names etc.
I didn't have the time in the end but I was going to set it up as follows;
/wp-content/themes/child/css/ ->/css/
/wp-content/themes/child/js/ -> /js/
/wp-content/themes/child/img/ -> /img/
/wp-content/themes/roots/css/ -> /css/bootstrap/
/wp-content/themes/roots/js/ -> /js/bootstrap/
/wp-content/themes/roots/img/ -> /img/bootstrap/
It just seemed more appropriate to have the framework in the subfolder as opposed to my site's content.
Uhm...
- latest wordpress release
- latest roots release
- child theme (only with style.css inside)
/css/bootstrap.css 404 (Not Found) /js/plugins.js 404 (Not Found) /css/app.css 404 (Not Found) /js/main.js 404 (Not Found) /css/bootstrap-responsive.css 404 (Not Found)
there's something wrong? (I'm too tired to understand today :) ) tnx
@taniot that's not related to this feature request, possibly the same issue as #478
tnx @retlehs :)
Back ontopic: is it possible to include the fix that Foxaii has provided in the trunk of Roots and the documentation? I'm willing to spend some time to fully test and adjust the docs if necessary.
The crew at NetDNA have made available free CDN-delivered Bootstrap assets. I wonder if this could be of help in the great Roots Child Themes debate? It might not be the best end solution, but it could offer some interim fix? http://www.bootstrapcdn.com/
- My point being, with a CDN available, the assets could be served from it, and there would be no need to deliver them from within Roots at all. That would eliminate the issue the rewriting the clean URIs, wouldn't it? Obviously serving them outside of your own environment is not ideal for everyone (additional DNS lookup, another SPOF, ect) but that might be the cost of child theming Roots.
I like the idea of adding the CDN, however I don't think that it solves this issue. With a CDN you would ideally want to fall back on local resources should the CDN fail. So you would need the correct rewrites anyway.
I also don't think there needs to be any cost of child theming Roots. All I did to get it working was edit a couple of lines in the htaccess/scripts inc files and manually edit the server's .htaccess config (see above).
I agree, there should not be any costs of child theming, maybe you can share some of the rewrite rules that you've used?
All the manual changes I made to .htaccess are listed above.
The php changes are as follows:
/inc/htaccess.php - Line 46
if (!is_multisite() && get_option('permalink_structure')) {
/inc/scripts.php - Line 16
wp_enqueue_style('roots_child_style', '/css/style.css';
Damn, I found out that I can't even get the normal child theme working. I got the following in roots/child/style.css:
/*
Theme Name: Roots Child
Theme URI: http://submid.nl
Description: Child theme for the Roots theme
Author: Dennis
Author URI: http://submid.nl
Template: roots
Version: 0.1.0
*/
@import url("/wp-content/themes/roots/style.css");
html {
background-color: red;
}
I can't see the child theme in the "Themes" page of the Administration section, which means I can't even enable it : /
The child theme should be in its own folder, in /wp-content/themes/ not in a subdirectory of Roots. You also needn't import the Roots style sheet as it contains no styling and will decrease performance.
RewriteRule ^css/(.*) /wp-content/themes/roots/css/$1 [QSA,L]
RewriteRule ^js/(.*) /wp-content/themes/roots/js/$1 [QSA,L]
RewriteRule ^img/(.*) /wp-content/themes/roots/img/$1 [QSA,L]
RewriteRule ^plugins/(.*) /wp-content/plugins/$1 [QSA,L]
Is how the rewrite rules end up inside .htaccess as it stands.
In the <head> Roots urls are written like:
/css/bootstrap.css
/css/app.css
/js/plugins.js
While child theme urls are written like:
/wp-content/themes/somechildtheme/style.css
/wp-content/themes/somechildtheme/somescript.js
/wp-content/themes/somechildtheme/somesubdir/another.js
/wp-content/themes/somechildtheme/css/another.css
What I think we can do is attempt to get rewrite rules that look (something) like:
RewriteRule ^css/(.*) /wp-content/themes/roots/css/$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/wp-content/themes/roots/css/(.*) /wp-content/themes/CHILD/$1 [QSA,L]
RewriteRule ^js/(.*) /wp-content/themes/roots/js/$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/wp-content/themes/roots/js/(.*) /wp-content/themes/CHILD/$1 [QSA,L]
RewriteRule ^img/(.*) /wp-content/themes/roots/img/$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/wp-content/themes/roots/img/(.*) /wp-content/themes/CHILD/$1 [QSA,L]
The above conditions probably don't work (I'm not a mod_rewrite expert) but the suggestion is we look in roots, then if the file doesn't exist we look in the child. This might mean roots would have to prefix file names or something to avoid conflicts.
A lingering issue is that we want all css, js or images to appear to be in a single directory, such that the child theme urls would be rewritten to:
/css/style.css
/js/somescript.js
/js/somesubdir/another.js
/css/another.css #note how this did not become /css/css/another.css
In order to do this, the roots_clean_urls function would need to detect the file extension and then prepend the folder, eg: /css/ for .css and /img/ for .png ... It would also need to be smart enough to not prepend the folder if the subdir were already in the child theme url (so as not to cause /css/css/).
So once the roots_clean_urls function is beefed up so that urls become /css/*.*, /js/*.*, /img/*.* or /css/somedir/*.* (etc) the rewrite rules could take those urls and look in the following directories in the following order: [roots] -> [child theme] -> [child theme]/[css/js/img]
I think for this solution to be a possibility $wp_rewrite is going to have to be dropped because it can't (as far as I know) do complex stuff like rewrite conditions.
Not to be nerdy but I was hoping for more discussion on this by now. You're all probably watching the Olympics 8).
Anyways, to get things rolling again, let me frame the three major questions associated with doing the [roots] -> [child theme] -> [child theme]/[css/js/img] approach I talked about:
First, Do we really need to use rewrite conditions (RewriteCond)? I assume the answer is yes.
Second, Can $wp_rewrite do rewrite conditions? I assume the answer is no.
Third, because of my mod_rewrite naïveté... What the heck should the rules look like?
I think that once we figure out what the rules need to look like we can work on how to actually get them into the .htaccess file correctly (read: in a WordPress-y way). Once that's accomplished we can work on the special cases such as naming conflicts or conventions...
I'm no good with mod_rewrite either, hence why I implemented the workaround using file prefixes.
My ideal solution would be that the .htaccess file is set up to look in the child folder and then the default roots folders. This is the same logic as templates etc.
I got as far as this, but didn't have the time to sort it:
RewriteCond %{DOCUMENT_ROOT}/wp-content/themes/child/$1 -f
RewriteRule ^(.*[^/])/?$ /wp-content/themes/child/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/wp-content/themes/roots/$1 -f
RewriteRule ^(.*[^/])/?$ /wp-content/themes/roots/$1 [QSA,L]
EDIT The above code now works. I can't vouch for performance though and it won't work for installs outside the root. If you use the child's style.css you will need to add:
RewriteRule ^css/style.css /wp-content/themes/child/style.css [QSA,L]
All together it looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{DOCUMENT_ROOT}/wp-content/themes/child/$1 -f
RewriteRule ^(.*[^/])/?$ /wp-content/themes/child/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/wp-content/themes/roots/$1 -f
RewriteRule ^(.*[^/])/?$ /wp-content/themes/roots/$1 [QSA,L]
RewriteRule ^css/style.css /wp-content/themes/child/style.css [QSA,L]
RewriteRule ^plugins/(.*) /wp-content/plugins/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Very good work @Foxaii - These are the rules that I used to great success:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{DOCUMENT_ROOT}/wp-content/themes/CHILDTHEMEDIRECTORY/$1 -f
RewriteRule ^(.*[^/])/?$ /wp-content/themes/CHILDTHEMEDIRECTORY/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/wp-content/themes/roots/$1 -f
RewriteRule ^(.*[^/])/?$ /wp-content/themes/roots/$1 [QSA,L]
RewriteRule ^plugins/(.*) /wp-content/plugins/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I didn't need the RewriteRule ^css/style.css /wp-content/themes/child/style.css [QSA,L] rule.
I did have to modify roots/inc/htaccess.php...
I added $content = str_replace(CHILD_THEME_PATH, '', $content); just before the return in roots_clean_urls(){} and I had to remove !is_multisite() && !is_child_theme() && in the if statement right after that same function definition.
I have modified my pull request #508 to use the rewrite conditions and rules proposed by @Foxaii