Created
January 6, 2014 13:32
-
-
Save Manoz/8282938 to your computer and use it in GitHub Desktop.
clean.php for WordPress. Clean wp_head() and other stuff like dashboard widgets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Clean wp_head() | |
* | |
* Remove feed links | |
* Remove extra feed links | |
* Remove RSD & Windows Live Writer links | |
* Remove WP version | |
* Remove nav links | |
*/ | |
function clean_head() { | |
// Originally from http://wpengineer.com/1438/wordpress-header/ | |
remove_action('wp_head', 'feed_links', 2); // Remove classic feeds links | |
remove_action('wp_head', 'feed_links_extra', 3); // Remove junk feeds links | |
remove_action('wp_head', 'rsd_link'); // Remove Really Simple Discovery link | |
remove_action('wp_head', 'wlwmanifest_link'); // Remove Windows Live Writer link | |
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); // Remove nav links | |
remove_action('wp_head', 'wp_generator'); // Remove WP Version | |
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); // Remove posts shortlinks | |
// Remove "Recents comments" default styles in <head> | |
global $wp_widget_factory; | |
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style')); | |
} | |
add_action('init', 'clean_head'); | |
/** | |
* Remove the WordPress version from RSS feeds | |
*/ | |
add_filter('the_generator', '__return_false'); | |
/** | |
* Clean up language_attributes() used in <html> tag | |
* | |
* Remove dir="ltr" | |
*/ | |
function clean_lang_attr() { | |
$attributes = array(); | |
$output = ''; | |
if (is_rtl()) { | |
$attributes[] = 'dir="rtl"'; | |
} | |
$lang = get_bloginfo('language'); | |
if ($lang) { | |
$attributes[] = "lang=\"$lang\""; | |
} | |
$output = implode(' ', $attributes); | |
$output = apply_filters('clean_lang_attr', $output); | |
return $output; | |
} | |
add_filter('language_attributes', 'clean_lang_attr'); | |
/** | |
* Clean up output of stylesheet <link> tags | |
* Before : <link rel='stylesheet' id='my-file-css' href='my-file.css' type='text/css' media='all' /> | |
* After : <link rel="stylesheet" href="my-file.css" /> (+ media="..." if required) | |
*/ | |
function clean_style($input) { | |
preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches); | |
// Only display media if it is meaningful | |
$media = $matches[3][0] !== '' && $matches[3][0] !== 'all' ? ' media="' . $matches[3][0] . '"' : ''; | |
return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n"; | |
} | |
add_filter('style_loader_tag', 'clean_style'); | |
/** | |
* Add and remove body_class() classes | |
*/ | |
function clean_body_class($classes) { | |
// 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', 'clean_body_class'); | |
/** | |
* Remove unnecessary dashboard widgets | |
* | |
* @link http://www.deluxeblogtips.com/2011/01/remove-dashboard-widgets-in-wordpress.html | |
*/ | |
function clean_dashboard_widgets() { | |
remove_meta_box('dashboard_plugins', 'dashboard', 'normal'); // Popular plugins | |
remove_meta_box('dashboard_primary', 'dashboard', 'normal'); // WordPress.org feeds | |
remove_meta_box('dashboard_secondary', 'dashboard', 'normal'); // WordPress news | |
remove_meta_box('dashboard_quick_press', 'dashboard', 'normal'); // Quick press (or quick draft) | |
remove_meta_box('dashboard_recent_drafts', 'dashboard', 'normal'); // Recent drafts | |
remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal'); // Incoming links | |
} | |
add_action('admin_init', 'clean_dashboard_widgets'); | |
/** | |
* Remove unnecessary self-closing tags | |
*/ | |
function clean_self_closing($input) { | |
return str_replace(' />', '>', $input); | |
} | |
add_filter('get_avatar', 'clean_self_closing'); // <img /> | |
add_filter('comment_id_fields', 'clean_self_closing'); // <input /> | |
add_filter('post_thumbnail_html', 'clean_self_closing'); // <img /> | |
/** | |
* Redirects search results from /?s=query to /search/query/, converts %20 to + | |
* Add "add_theme_support('nice-search');" after your 'after_setup_theme' action. | |
* | |
* @link http://txfx.net/wordpress-plugins/nice-search/ | |
*/ | |
function nice_search() { | |
global $wp_rewrite; | |
if (!isset($wp_rewrite) || !is_object($wp_rewrite) || !$wp_rewrite->using_permalinks()) { | |
return; | |
} | |
$search_base = $wp_rewrite->search_base; | |
if (is_search() && !is_admin() && strpos($_SERVER['REQUEST_URI'], "/{$search_base}/") === false) { | |
wp_redirect(home_url("/{$search_base}/" . urlencode(get_query_var('s')))); | |
exit(); | |
} | |
} | |
if (current_theme_supports('nice-search')) { | |
add_action('template_redirect', 'nice_search'); | |
} | |
/** | |
* Don't return the default description in the RSS feed if it hasn't been changed | |
*/ | |
function clean_default_desc($bloginfo) { | |
$default_tagline = 'Just another WordPress site'; | |
return ($bloginfo === $default_tagline) ? '' : $bloginfo; | |
} | |
add_filter('get_bloginfo_rss', 'clean_default_desc'); | |
/** | |
* Remove wp version param from any enqueued scripts | |
* Used this function when you build your theme. | |
*/ | |
function clean_wp_ver_css_js( $src ) { | |
if ( strpos( $src, 'ver=' ) ) | |
$src = remove_query_arg( 'ver', $src ); | |
return $src; | |
} | |
add_filter( 'style_loader_src', 'clean_wp_ver_css_js', 9999 ); | |
add_filter( 'script_loader_src', 'clean_wp_ver_css_js', 9999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment