Rewrites organization

- Create lib/rewrites.php and move everything from lib/htaccess.php
except the H5BP htaccess functionality into it

- In the config, change add_theme_support('rewrite-urls') to
add_theme_support('rewrites')

- Remove the Apache/LightSpeed HTTPD check for rewrites

- Update docs with better Nginx rewrite rules
This commit is contained in:
Ben Word
2013-02-04 19:56:42 -06:00
parent df6a72e839
commit fea92a61aa
8 changed files with 148 additions and 143 deletions

View File

@@ -45,7 +45,7 @@ This file contains HTML5 Boilerplate's `.htaccess` which is automatically added
### htaccess.php ### htaccess.php
This file handles the clean URL rewrites and HTML5 Boilerplate `.htaccess`. [About the rewrites](rewrites.md). This file handles the HTML5 Boilerplate `.htaccess`.
### init.php ### init.php
@@ -55,6 +55,10 @@ This file runs the initial theme setup and defines helper constants for later us
This file contains all the custom nav modifications (for Bootstrap) and clean up. This file contains all the custom nav modifications (for Bootstrap) and clean up.
### rewrites.php
This file handles the clean URL rewrites. [About the rewrites](rewrites.md).
### scripts.php ### scripts.php
This file handles all of the CSS and JavaScript. This file handles all of the CSS and JavaScript.

View File

@@ -3,7 +3,7 @@ table of contents](TOC.md)
# Rewrites # Rewrites
Rewrites are handled by `lib/htaccess.php`. Rewrites currently do not happen for child themes or network installs. Rewrites are handled by `lib/rewrites.php`. Rewrites currently do not happen for child themes or network installs.
Rewrite: Rewrite:
@@ -14,18 +14,15 @@ Rewrite:
If HTML5 Boilerplate's `.htaccess` support is enabled in `lib/config.php`, then the `generate_rewrite_rules()` filter is used to automatically add the contents of `lib/h5bp-htaccess` to your `.htaccess` file. If HTML5 Boilerplate's `.htaccess` support is enabled in `lib/config.php`, then the `generate_rewrite_rules()` filter is used to automatically add the contents of `lib/h5bp-htaccess` to your `.htaccess` file.
## Alternative configuration ## Alternative server configurations
First remove the `if` statement that wraps everything, since if you're not on Apache or Litespeed then Roots will not apply the functionality.
### Nginx ### Nginx
if (!-e $request_filename) { location ~ ^/assets/(img|js|css)/(.*)$ {
rewrite ^/assets/css/(.*)$ /wp-content/themes/roots/assets/css/$1 last; try_files $uri $uri/ /wp-content/themes/roots/assets/$1/$2;
rewrite ^/assets/js/(.*)$ /wp-content/themes/roots/assets/js/$1 last; }
rewrite ^/assets/img/(.*)$ /wp-content/themes/roots/assets/img/$1 last; location ~ ^/plugins/(.*)$ {
rewrite ^/plugins/(.*)$ /wp-content/plugins/$1 last; try_files $uri $uri/ /wp-content/plugins/$1;
break;
} }
### Lighttpd ### Lighttpd

View File

@@ -42,6 +42,7 @@ A basic Roots theme initially looks like this:
│ ├── htaccess.php │ ├── htaccess.php
│ ├── init.php │ ├── init.php
│ ├── nav.php │ ├── nav.php
│ ├── rewrites.php
│ ├── scripts.php │ ├── scripts.php
│ ├── sidebar.php │ ├── sidebar.php
│ ├── utils.php │ ├── utils.php

View File

@@ -12,7 +12,8 @@ require_once locate_template('/lib/config.php'); // Configuration
require_once locate_template('/lib/activation.php'); // Theme activation require_once locate_template('/lib/activation.php'); // Theme activation
require_once locate_template('/lib/cleanup.php'); // Cleanup require_once locate_template('/lib/cleanup.php'); // Cleanup
require_once locate_template('/lib/nav.php'); // Custom nav modifications require_once locate_template('/lib/nav.php'); // Custom nav modifications
require_once locate_template('/lib/htaccess.php'); // Rewrites for assets, H5BP .htaccess require_once locate_template('/lib/rewrites.php'); // URL rewriting for assets
require_once locate_template('/lib/htaccess.php'); // HTML5 Boilerplate .htaccess
require_once locate_template('/lib/widgets.php'); // Sidebars and widgets require_once locate_template('/lib/widgets.php'); // Sidebars and widgets
require_once locate_template('/lib/scripts.php'); // Scripts and stylesheets require_once locate_template('/lib/scripts.php'); // Scripts and stylesheets
require_once locate_template('/lib/custom.php'); // Custom functions require_once locate_template('/lib/custom.php'); // Custom functions

View File

@@ -5,12 +5,11 @@
// Enable theme features // Enable theme features
add_theme_support('root-relative-urls'); // Enable relative URLs add_theme_support('root-relative-urls'); // Enable relative URLs
add_theme_support('rewrite-urls'); // Enable URL rewrites add_theme_support('rewrites'); // Enable URL rewrites
add_theme_support('h5bp-htaccess'); // Enable HTML5 Boilerplate's .htaccess add_theme_support('h5bp-htaccess'); // Enable HTML5 Boilerplate's .htaccess
add_theme_support('bootstrap-top-navbar'); // Enable Bootstrap's fixed navbar add_theme_support('bootstrap-top-navbar'); // Enable Bootstrap's fixed navbar
add_theme_support('nice-search'); // Enable /?s= to /search/ redirect add_theme_support('nice-search'); // Enable /?s= to /search/ redirect
/** /**
* Define which pages shouldn't have the sidebar * Define which pages shouldn't have the sidebar
* *
@@ -65,12 +64,10 @@ define('GOOGLE_ANALYTICS_ID', ''); // UA-XXXXX-Y
define('POST_EXCERPT_LENGTH', 40); define('POST_EXCERPT_LENGTH', 40);
/** /**
* $content_width is a global variable used by WordPress for max image upload sizes and media embeds (in pixels) * $content_width is a global variable used by WordPress for max image upload sizes
* * and media embeds (in pixels).
* Example: If the content area is 640px wide, set $content_width = 620; so images and videos will not overflow. *
* * Example: If the content area is 640px wide, set $content_width = 620; so images and videos will not overflow.
* Default: 940px is the default Bootstrap container width. * Default: 940px is the default Bootstrap container width.
* */
* This is not required or used by Roots.
*/
if (!isset($content_width)) { $content_width = 940; } if (!isset($content_width)) { $content_width = 940; }

View File

@@ -1,95 +1,28 @@
<?php <?php
/** /**
* URL rewriting and addition of HTML5 Boilerplate's .htaccess * Add HTML5 Boilerplate's .htaccess via WordPress
*
* Rewrites currently do not happen for child themes (or network installs)
* @todo https://github.com/retlehs/roots/issues/461
*
* Rewrite:
* /wp-content/themes/themename/css/ to /css/
* /wp-content/themes/themename/js/ to /js/
* /wp-content/themes/themename/img/ to /img/
* /wp-content/plugins/ to /plugins/
*
* If you aren't using Apache, alternate configuration settings can be found in the docs.
*
* @link https://github.com/retlehs/roots/blob/master/doc/rewrites.md
*/ */
if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') || stristr($_SERVER['SERVER_SOFTWARE'], 'litespeed') !== false) { // Add the contents of h5bp-htaccess into .htaccess
function roots_add_h5bp_htaccess($content) {
global $wp_rewrite;
$home_path = function_exists('get_home_path') ? get_home_path() : ABSPATH;
$htaccess_file = $home_path . '.htaccess';
$mod_rewrite_enabled = function_exists('got_mod_rewrite') ? got_mod_rewrite() : false;
// Show an admin notice if .htaccess isn't writable if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
function roots_htaccess_writable() { if ($mod_rewrite_enabled) {
if (!is_writable(get_home_path() . '.htaccess')) { $h5bp_rules = extract_from_markers($htaccess_file, 'HTML5 Boilerplate');
if (current_user_can('administrator')) { if ($h5bp_rules === array()) {
add_action('admin_notices', create_function('', "echo '<div class=\"error\"><p>" . sprintf(__('Please make sure your <a href="%s">.htaccess</a> file is writable ', 'roots'), admin_url('options-permalink.php')) . "</p></div>';")); $filename = dirname(__FILE__) . '/h5bp-htaccess';
return insert_with_markers($htaccess_file, 'HTML5 Boilerplate', extract_from_markers($filename, 'HTML5 Boilerplate'));
} }
} }
} }
add_action('admin_init', 'roots_htaccess_writable'); return $content;
}
function roots_add_rewrites($content) { if (current_theme_supports('h5bp-htaccess')) {
global $wp_rewrite; add_action('generate_rewrite_rules', 'roots_add_h5bp_htaccess');
$roots_new_non_wp_rules = array( }
'assets/css/(.*)' => THEME_PATH . '/assets/css/$1',
'assets/js/(.*)' => THEME_PATH . '/assets/js/$1',
'assets/img/(.*)' => THEME_PATH . '/assets/img/$1',
'plugins/(.*)' => RELATIVE_PLUGIN_PATH . '/$1'
);
$wp_rewrite->non_wp_rules = array_merge($wp_rewrite->non_wp_rules, $roots_new_non_wp_rules);
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);
}
}
if (!is_multisite() && !is_child_theme() && get_option('permalink_structure')) {
if (current_theme_supports('rewrite-urls')) {
add_action('generate_rewrite_rules', 'roots_add_rewrites');
}
if (current_theme_supports('h5bp-htaccess')) {
add_action('generate_rewrite_rules', 'roots_add_h5bp_htaccess');
}
if (!is_admin() && current_theme_supports('rewrite-urls')) {
$tags = array(
'plugins_url',
'bloginfo',
'stylesheet_directory_uri',
'template_directory_uri',
'script_loader_src',
'style_loader_src'
);
add_filters($tags, 'roots_clean_urls');
}
}
// Add the contents of h5bp-htaccess into the .htaccess file
function roots_add_h5bp_htaccess($content) {
global $wp_rewrite;
$home_path = function_exists('get_home_path') ? get_home_path() : ABSPATH;
$htaccess_file = $home_path . '.htaccess';
$mod_rewrite_enabled = function_exists('got_mod_rewrite') ? got_mod_rewrite() : false;
if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
if ($mod_rewrite_enabled) {
$h5bp_rules = extract_from_markers($htaccess_file, 'HTML5 Boilerplate');
if ($h5bp_rules === array()) {
$filename = dirname(__FILE__) . '/h5bp-htaccess';
return insert_with_markers($htaccess_file, 'HTML5 Boilerplate', extract_from_markers($filename, 'HTML5 Boilerplate'));
}
}
}
return $content;
}
}

56
lib/rewrites.php Normal file
View File

@@ -0,0 +1,56 @@
<?php
/**
* URL rewriting
*
* Rewrites currently do not happen for child themes (or network installs)
* @todo https://github.com/retlehs/roots/issues/461
*
* Rewrite:
* /wp-content/themes/themename/css/ to /css/
* /wp-content/themes/themename/js/ to /js/
* /wp-content/themes/themename/img/ to /img/
* /wp-content/plugins/ to /plugins/
*
* If you aren't using Apache, alternate configuration settings can be found in the docs.
*
* @link https://github.com/retlehs/roots/blob/master/doc/rewrites.md
*/
function roots_add_rewrites($content) {
global $wp_rewrite;
$roots_new_non_wp_rules = array(
'assets/css/(.*)' => THEME_PATH . '/assets/css/$1',
'assets/js/(.*)' => THEME_PATH . '/assets/js/$1',
'assets/img/(.*)' => THEME_PATH . '/assets/img/$1',
'plugins/(.*)' => RELATIVE_PLUGIN_PATH . '/$1'
);
$wp_rewrite->non_wp_rules = array_merge($wp_rewrite->non_wp_rules, $roots_new_non_wp_rules);
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);
}
}
if (!is_multisite() && !is_child_theme() && get_option('permalink_structure')) {
if (current_theme_supports('rewrites')) {
add_action('generate_rewrite_rules', 'roots_add_rewrites');
}
if (!is_admin() && current_theme_supports('rewrites')) {
$tags = array(
'plugins_url',
'bloginfo',
'stylesheet_directory_uri',
'template_directory_uri',
'script_loader_src',
'style_loader_src'
);
add_filters($tags, 'roots_clean_urls');
}
}

View File

@@ -5,42 +5,6 @@
* *
* @link http://scribu.net/wordpress/theme-wrappers.html * @link http://scribu.net/wordpress/theme-wrappers.html
*/ */
function roots_title() {
if (is_home()) {
if (get_option('page_for_posts', true)) {
echo get_the_title(get_option('page_for_posts', true));
} else {
_e('Latest Posts', 'roots');
}
} elseif (is_archive()) {
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
if ($term) {
echo $term->name;
} elseif (is_post_type_archive()) {
echo get_queried_object()->labels->name;
} elseif (is_day()) {
printf(__('Daily Archives: %s', 'roots'), get_the_date());
} elseif (is_month()) {
printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
} elseif (is_year()) {
printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
} elseif (is_author()) {
global $post;
$author_id = $post->post_author;
printf(__('Author Archives: %s', 'roots'), get_the_author_meta('display_name', $author_id));
} else {
single_cat_title();
}
} elseif (is_search()) {
printf(__('Search Results for %s', 'roots'), get_search_query());
} elseif (is_404()) {
_e('File Not Found', 'roots');
} else {
the_title();
}
}
function roots_template_path() { function roots_template_path() {
return Roots_Wrapping::$main_template; return Roots_Wrapping::$main_template;
} }
@@ -87,6 +51,58 @@ class Roots_Wrapping {
add_filter('template_include', array('Roots_Wrapping', 'wrap'), 99); add_filter('template_include', array('Roots_Wrapping', 'wrap'), 99);
/**
* Page titles
*/
function roots_title() {
if (is_home()) {
if (get_option('page_for_posts', true)) {
echo get_the_title(get_option('page_for_posts', true));
} else {
_e('Latest Posts', 'roots');
}
} elseif (is_archive()) {
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
if ($term) {
echo $term->name;
} elseif (is_post_type_archive()) {
echo get_queried_object()->labels->name;
} elseif (is_day()) {
printf(__('Daily Archives: %s', 'roots'), get_the_date());
} elseif (is_month()) {
printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
} elseif (is_year()) {
printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
} elseif (is_author()) {
global $post;
$author_id = $post->post_author;
printf(__('Author Archives: %s', 'roots'), get_the_author_meta('display_name', $author_id));
} else {
single_cat_title();
}
} elseif (is_search()) {
printf(__('Search Results for %s', 'roots'), get_search_query());
} elseif (is_404()) {
_e('File Not Found', 'roots');
} else {
the_title();
}
}
/**
* Show an admin notice if .htaccess isn't writable
*/
function roots_htaccess_writable() {
if (!is_writable(get_home_path() . '.htaccess')) {
if (current_user_can('administrator')) {
add_action('admin_notices', create_function('', "echo '<div class=\"error\"><p>" . sprintf(__('Please make sure your <a href="%s">.htaccess</a> file is writable ', 'roots'), admin_url('options-permalink.php')) . "</p></div>';"));
}
}
}
add_action('admin_init', 'roots_htaccess_writable');
// returns WordPress subdirectory if applicable // returns WordPress subdirectory if applicable
function wp_base_dir() { function wp_base_dir() {
preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches); preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches);