Cleanup clean up, reorganization

This commit is contained in:
Ben Word
2012-09-03 09:43:38 -05:00
parent 141cb513a5
commit f538aa0116

View File

@@ -1,49 +1,127 @@
<?php <?php
/** /**
* Redirects search results from /?s=query to /search/query/, converts %20 to + * Clean up wp_head()
* *
* @link http://txfx.net/wordpress-plugins/nice-search/ * Remove unnecessary <link>'s
* Remove inline CSS used by Recent Comments widget
* Remove inline CSS used by posts with galleries
* Remove self-closing tag and change ''s to "'s on rel_canonical()
*/ */
function roots_nice_search_redirect() { function roots_head_cleanup() {
if (is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false) { // Originally from http://wpengineer.com/1438/wordpress-header/
wp_redirect(home_url('/search/' . str_replace(array(' ', '%20'), array('+', '+'), urlencode(get_query_var('s')))), 301); remove_action('wp_head', 'feed_links', 2);
exit(); remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
global $wp_widget_factory;
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
add_filter('use_default_gallery_style', '__return_null');
if (!class_exists('WPSEO_Frontend')) {
remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'roots_rel_canonical');
} }
} }
add_action('template_redirect', 'roots_nice_search_redirect'); function roots_rel_canonical() {
global $wp_the_query;
if (!is_singular()) {
return;
}
if (!$id = $wp_the_query->get_queried_object_id()) {
return;
}
$link = get_permalink($id);
echo "\t<link rel=\"canonical\" href=\"$link\">\n";
}
add_action('init', 'roots_head_cleanup');
/** /**
* Fix for get_search_query() returning +'s between search terms * Remove the WordPress version from RSS feeds
*/ */
function roots_search_query($escaped = true) { add_filter('the_generator', '__return_false');
$query = apply_filters('roots_search_query', get_query_var('s'));
if ($escaped) {
$query = esc_attr($query);
}
return urldecode($query);
}
add_filter('get_search_query', 'roots_search_query');
/** /**
* Fix for empty search queries redirecting to home page * Clean up language_attributes() used in <html> tag
* *
* @link http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565 * Change lang="en-US" to lang="en"
* @link http://core.trac.wordpress.org/ticket/11330 * Remove dir="ltr"
*/ */
function roots_request_filter($query_vars) { function roots_language_attributes() {
if (isset($_GET['s']) && empty($_GET['s'])) { $attributes = array();
$query_vars['s'] = ' '; $output = '';
if (function_exists('is_rtl')) {
if (is_rtl() == 'rtl') {
$attributes[] = 'dir="rtl"';
}
} }
return $query_vars; $lang = get_bloginfo('language');
if ($lang && $lang !== 'en-US') {
$attributes[] = "lang=\"$lang\"";
} else {
$attributes[] = 'lang="en"';
} }
add_filter('request', 'roots_request_filter'); $output = implode(' ', $attributes);
$output = apply_filters('roots_language_attributes', $output);
return $output;
}
add_filter('language_attributes', 'roots_language_attributes');
/**
* Clean up output of stylesheet <link> tags
*/
function roots_clean_style_tag($input) {
preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
// Only display media if it's print
$media = $matches[3][0] === 'print' ? ' media="print"' : '';
return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
}
add_filter('style_loader_tag', 'roots_clean_style_tag');
/**
* Add and remove body_class() classes
*/
function roots_body_class($classes) {
// Add 'top-navbar' class if using Bootstrap's Navbar
// Used to add styling to account for the WordPress admin bar
if (current_theme_supports('bootstrap-top-navbar')) {
$classes[] = 'top-navbar';
}
// Add post/page slug
if (is_single() || is_page() && !is_front_page()) {
$classes[] = basename(get_permalink());
}
// Remove unnecessary classes
$home_id_class = 'page-id-' . get_option('page_on_front');
$remove_classes = array(
'page-template-default',
$home_id_class
);
$classes = array_diff($classes, $remove_classes);
return $classes;
}
add_filter('body_class', 'roots_body_class');
/** /**
* Root relative URLs * Root relative URLs
@@ -91,11 +169,11 @@ function roots_fix_duplicate_subfolder_urls($input) {
return $output; return $output;
} }
function enable_root_relative_urls() { function roots_enable_root_relative_urls() {
return !(is_admin() && in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) && current_theme_supports('root-relative-urls'); return !(is_admin() && in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) && current_theme_supports('root-relative-urls');
} }
if (enable_root_relative_urls()) { if (roots_enable_root_relative_urls()) {
$root_rel_filters = array( $root_rel_filters = array(
'bloginfo_url', 'bloginfo_url',
'theme_root_uri', 'theme_root_uri',
@@ -124,95 +202,68 @@ if (enable_root_relative_urls()) {
} }
/** /**
* Cleanup language_attributes() used in <html> tag * Wrap embedded media as suggested by Readability
* *
* Change lang="en-US" to lang="en" * @link https://gist.github.com/965956
* Remove dir="ltr" * @link http://www.readability.com/publishers/guidelines#publisher
*/ */
function roots_language_attributes() { function roots_embed_wrap($cache, $url, $attr = '', $post_ID = '') {
$attributes = array(); return '<div class="entry-content-asset">' . $cache . '</div>';
$output = '';
if (function_exists('is_rtl')) {
if (is_rtl() == 'rtl') {
$attributes[] = 'dir="rtl"';
}
} }
$lang = get_bloginfo('language'); add_filter('embed_oembed_html', 'roots_embed_wrap', 10, 4);
add_filter('embed_googlevideo', 'roots_embed_wrap', 10, 2);
if ($lang && $lang !== 'en-US') { /**
$attributes[] = "lang=\"$lang\""; * Add class="thumbnail" to attachment items
} else { */
$attributes[] = 'lang="en"'; function roots_attachment_link_class($html) {
$postid = get_the_ID();
$html = str_replace('<a', '<a class="thumbnail"', $html);
return $html;
} }
$output = implode(' ', $attributes); add_filter('wp_get_attachment_link', 'roots_attachment_link_class', 10, 1);
$output = apply_filters('roots_language_attributes', $output);
/**
* Add Bootstrap thumbnail styling to images with captions
* Use <figure> and <figcaption>
*
* @link http://justintadlock.com/archives/2011/07/01/captions-in-wordpress
*/
function roots_caption($output, $attr, $content) {
if (is_feed()) {
return $output;
}
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
$attr = shortcode_atts($defaults, $attr);
// If the width is less than 1 or there is no caption, return the content wrapped between the [caption] tags
if (1 > $attr['width'] || empty($attr['caption'])) {
return $content;
}
// Set up the attributes for the caption <figure>
$attributes = (!empty($attr['id']) ? ' id="' . esc_attr($attr['id']) . '"' : '' );
$attributes .= ' class="thumbnail wp-caption ' . esc_attr($attr['align']) . '"';
$attributes .= ' style="width: ' . esc_attr($attr['width']) . 'px"';
$output = '<figure' . $attributes .'>';
$output .= do_shortcode($content);
$output .= '<figcaption class="caption wp-caption-text">' . $attr['caption'] . '</figcaption>';
$output .= '</figure>';
return $output; return $output;
} }
add_filter('language_attributes', 'roots_language_attributes'); add_filter('img_caption_shortcode', 'roots_caption', 10, 3);
/**
* Remove the WordPress version from RSS feeds
*/
function roots_remove_generator() { return; }
add_filter('the_generator', 'roots_remove_generator');
/**
* Cleanup wp_head()
*
* Remove unnecessary <link>'s
* Remove inline CSS used by Recent Comments widget
* Remove inline CSS used by posts with galleries
* Remove self-closing tag and change ''s to "'s on rel_canonical()
*/
function roots_head_cleanup() {
// http://wpengineer.com/1438/wordpress-header/
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
add_action('wp_head', 'roots_remove_recent_comments_style', 1);
add_filter('use_default_gallery_style', '__return_null');
if (!class_exists('WPSEO_Frontend')) {
remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'roots_rel_canonical');
}
}
function roots_rel_canonical() {
global $wp_the_query;
if (!is_singular()) {
return;
}
if (!$id = $wp_the_query->get_queried_object_id()) {
return;
}
$link = get_permalink($id);
echo "\t<link rel=\"canonical\" href=\"$link\">\n";
}
function roots_remove_recent_comments_style() {
global $wp_widget_factory;
if (isset($wp_widget_factory->widgets['WP_Widget_Recent_Comments'])) {
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
}
}
add_action('init', 'roots_head_cleanup');
/** /**
* Clean up gallery_shortcode() * Clean up gallery_shortcode()
@@ -328,57 +379,6 @@ function roots_gallery($attr) {
remove_shortcode('gallery'); remove_shortcode('gallery');
add_shortcode('gallery', 'roots_gallery'); add_shortcode('gallery', 'roots_gallery');
/**
* Add class="thumbnail" to attachment items
*/
function roots_attachment_link_class($html) {
$postid = get_the_ID();
$html = str_replace('<a', '<a class="thumbnail"', $html);
return $html;
}
add_filter('wp_get_attachment_link', 'roots_attachment_link_class', 10, 1);
/**
* Add Bootstrap thumbnail styling to images with captions
* Use <figure> and <figcaption>
*
* @link http://justintadlock.com/archives/2011/07/01/captions-in-wordpress
*/
function roots_caption($output, $attr, $content) {
if (is_feed()) {
return $output;
}
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
$attr = shortcode_atts($defaults, $attr);
// If the width is less than 1 or there is no caption, return the content wrapped between the [caption] tags
if (1 > $attr['width'] || empty($attr['caption'])) {
return $content;
}
// Set up the attributes for the caption <div>
$attributes = (!empty($attr['id']) ? ' id="' . esc_attr($attr['id']) . '"' : '' );
$attributes .= ' class="thumbnail wp-caption ' . esc_attr($attr['align']) . '"';
$attributes .= ' style="width: ' . esc_attr($attr['width']) . 'px"';
$output = '<figure' . $attributes .'>';
$output .= do_shortcode($content);
$output .= '<figcaption class="caption wp-caption-text">' . $attr['caption'] . '</figcaption>';
$output .= '</figure>';
return $output;
}
add_filter('img_caption_shortcode', 'roots_caption', 10, 3);
/** /**
* Remove unnecessary dashboard widgets * Remove unnecessary dashboard widgets
* *
@@ -534,6 +534,7 @@ function roots_nav_menu_args($args = '') {
$roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>'; $roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
} }
// Bootstrap's navbar doesn't support multi-level dropdowns
if (current_theme_supports('bootstrap-top-navbar')) { if (current_theme_supports('bootstrap-top-navbar')) {
$roots_nav_menu_args['depth'] = 2; $roots_nav_menu_args['depth'] = 2;
} }
@@ -547,7 +548,6 @@ function roots_nav_menu_args($args = '') {
add_filter('wp_nav_menu_args', 'roots_nav_menu_args'); add_filter('wp_nav_menu_args', 'roots_nav_menu_args');
/** /**
* Remove unnecessary self-closing tags * Remove unnecessary self-closing tags
*/ */
@@ -587,31 +587,6 @@ function roots_change_mce_options($options) {
add_filter('tiny_mce_before_init', 'roots_change_mce_options'); add_filter('tiny_mce_before_init', 'roots_change_mce_options');
/**
* Cleanup output of stylesheet <link> tags
*/
add_filter('style_loader_tag', 'roots_clean_style_tag');
function roots_clean_style_tag($input) {
preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
// Only display media if it's print
$media = $matches[3][0] === 'print' ? ' media="print"' : '';
return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
}
/**
* Apply filters to body_class()
*/
function roots_body_class_filter($classes) {
// Add 'top-navbar' class if using Bootstrap's Navbar
if (current_theme_supports('bootstrap-top-navbar')) {
$classes[] = 'top-navbar';
}
return $classes;
}
add_filter('body_class', 'roots_body_class_filter');
/** /**
* Add additional classes onto widgets * Add additional classes onto widgets
* *
@@ -648,23 +623,54 @@ function roots_widget_first_last_classes($params) {
$params[0]['before_widget'] = preg_replace('/class=\"/', "$class", $params[0]['before_widget'], 1); $params[0]['before_widget'] = preg_replace('/class=\"/', "$class", $params[0]['before_widget'], 1);
return $params; return $params;
} }
add_filter('dynamic_sidebar_params', 'roots_widget_first_last_classes'); add_filter('dynamic_sidebar_params', 'roots_widget_first_last_classes');
/** /**
* Wrap embedded media as suggested by Readability * Redirects search results from /?s=query to /search/query/, converts %20 to +
* *
* @link https://gist.github.com/965956 * @link http://txfx.net/wordpress-plugins/nice-search/
* @link http://www.readability.com/publishers/guidelines#publisher
*/ */
function roots_embed_wrap($cache, $url, $attr = '', $post_ID = '') { function roots_nice_search_redirect() {
return '<div class="entry-content-asset">' . $cache . '</div>'; if (is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false) {
wp_redirect(home_url('/search/' . str_replace(array(' ', '%20'), array('+', '+'), urlencode(get_query_var('s')))), 301);
exit();
}
} }
add_filter('embed_oembed_html', 'roots_embed_wrap', 10, 4); add_action('template_redirect', 'roots_nice_search_redirect');
add_filter('embed_googlevideo', 'roots_embed_wrap', 10, 2);
/**
* Fix for get_search_query() returning +'s between search terms
*/
function roots_search_query($escaped = true) {
$query = apply_filters('roots_search_query', get_query_var('s'));
if ($escaped) {
$query = esc_attr($query);
}
return urldecode($query);
}
add_filter('get_search_query', 'roots_search_query');
/**
* Fix for empty search queries redirecting to home page
*
* @link http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565
* @link http://core.trac.wordpress.org/ticket/11330
*/
function roots_request_filter($query_vars) {
if (isset($_GET['s']) && empty($_GET['s'])) {
$query_vars['s'] = ' ';
}
return $query_vars;
}
add_filter('request', 'roots_request_filter');
/** /**
* Tell WordPress to use searchform.php from the templates/ directory * Tell WordPress to use searchform.php from the templates/ directory