Skip to content

Instantly share code, notes, and snippets.

@martinsanne
Created December 8, 2015 10:54
Show Gist options
  • Save martinsanne/e7fad2ea43e03992a8e2 to your computer and use it in GitHub Desktop.
Save martinsanne/e7fad2ea43e03992a8e2 to your computer and use it in GitHub Desktop.
WordPress cleanup
<?php
function wpbase_remove_admin_bar_glitch(){
// make sure body is not relative positioned
if ( is_admin_bar_showing() ) { ?>
<style type="text/css"> body { position: static !important; } </style>
<?php }
}
add_action('wp_head', 'wpbase_remove_admin_bar_glitch');
if ( ! function_exists( 'wpbase_start_cleanup' ) ) :
function wpbase_start_cleanup() {
// Launching operation cleanup.
add_action( 'init', 'wpbase_cleanup_head' );
// Remove WP version from RSS.
add_filter( 'the_generator', 'wpbase_remove_rss_version' );
// Remove pesky injected css for recent comments widget.
add_filter( 'wp_head', 'wpbase_remove_wp_widget_recent_comments_style', 1 );
// Clean up comment styles in the head.
add_action( 'wp_head', 'wpbase_remove_recent_comments_style', 1 );
// Clean up gallery output in wp.
add_filter( 'wpbase_gallery_style', 'wpbase_gallery_style' );
// Additional post related cleaning.
add_filter( 'get_wpbase_image_tag_class', 'wpbase_image_tag_class', 0, 4 );
add_filter( 'get_image_tag', 'wpbase_image_editor', 0, 4 );
add_filter( 'the_content', 'img_unautop', 30 );
// Remove default image link
update_option( 'image_default_link_type', 'none' );
}
add_action( 'after_setup_theme','wpbase_start_cleanup' );
endif;
if ( ! function_exists( 'wpbase_cleanup_head' ) ) :
function wpbase_cleanup_head() {
// EditURI link.
remove_action( 'wp_head', 'rsd_link' );
// Category feed links.
remove_action( 'wp_head', 'feed_links_extra', 3 );
// Post and comment feed links.
remove_action( 'wp_head', 'feed_links', 2 );
// Windows Live Writer.
remove_action( 'wp_head', 'wlwmanifest_link' );
// Index link.
remove_action( 'wp_head', 'index_rel_link' );
// Previous link.
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
// Start link.
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
// Canonical.
remove_action( 'wp_head', 'rel_canonical', 10, 0 );
// Shortlink.
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
// Links for adjacent posts.
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
// WP version.
remove_action( 'wp_head', 'wp_generator' );
// Emoji detection script.
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
// Emoji styles.
remove_action( 'wp_print_styles', 'print_emoji_styles' );
}
endif;
// Remove injected CSS for recent comments widget.
if ( ! function_exists( 'wpbase_remove_wp_widget_recent_comments_style' ) ) :
function wpbase_remove_wp_widget_recent_comments_style() {
if ( has_filter( 'wp_head', 'wp_widget_recent_comments_style' ) ) {
remove_filter( 'wp_head', 'wp_widget_recent_comments_style' );
}
}
endif;
// Remove injected CSS from recent comments widget.
if ( ! function_exists( 'wpbase_remove_recent_comments_style' ) ) :
function wpbase_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') );
}
}
endif;
// Remove width and height in editor, for a better responsive world.
if ( ! function_exists( 'wpbase_image_editor' ) ) :
function wpbase_image_editor($html, $id, $alt, $title) {
return preg_replace(array(
'/\s+width="\d+"/i',
'/\s+height="\d+"/i',
'/alt=""/i',
),
array(
'',
'',
'',
'alt="' . $title . '"',
),
$html);
}
endif;
// Remove injected CSS from gallery.
if ( ! function_exists( 'wpbase_gallery_style' ) ) :
function wpbase_gallery_style($css) {
return preg_replace( "!<style type='text/css'>(.*?)</style>!s", '', $css );
}
endif;
// Wrap images with figure tag - Credit: Robert O'Rourke - http://bit.ly/1q0WHFs .
if ( ! function_exists( 'img_unauto' ) ) :
function img_unautop($pee) {
$pee = preg_replace( '/<p>\\s*?(<a .*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s', '<figure>$1</figure>', $pee );
return $pee;
}
endif;
// Remove empty paragraphs inside shortcodes
function shortcode_empty_paragraph_fix( $content ) {
$array = array(
'<p>[' => '[',
']</p>' => ']',
']<br />' => ']'
);
return strtr( $content, $array );
}
add_filter( 'the_content', 'shortcode_empty_paragraph_fix' );
// remove stale attributes from iframe embeds
function oembed_remove_atts_filter( $return, $data, $url ) {
$return = str_replace('frameborder="0" allowfullscreen', 'style="border: none"', $return);
return $return;
}
add_filter('oembed_dataparse', 'oembed_remove_atts_filter', 90, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment