My WordPress 'cleanup' script

I include the follow­ing, or some­thing similar, in most of my Word­Press projects. It stand­ard­ises some awkward beha­viours, removes some super­flu­ous settings and capab­il­it­ies, and enhances/​improves some default func­tions and approaches.

Warn­ing – do not simply copy and paste this into your theme/​functions; it’ll need adapt­ing to fit your use-case, and will likely cause as many prob­lems as it solves if not tweaked and refined to fit your needs.

class WP_Cleanup {

  public function clean_cruft() {

    // Init stuff
    remove_action('init', 'wp_widgets_init', 1);

    // Head stuff
    remove_action( 'wp_head', 'rsd_link');
    remove_action( 'wp_head', 'wlwmanifest_link');
    remove_action( 'wp_head', 'wp_shortlink_wp_head');
    remove_action( 'wp_head', 'wp_generator');
    remove_action( 'wp_head', 'feed_links', 2 );
    remove_action( 'wp_head', 'feed_links_extra', 3 );
    remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
    remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
    remove_action( 'template_redirect', 'wp_shortlink_header', 99 );

    // Plugin cruft
    remove_action( 'wp_head', 'adrotate_custom_css' );

    // Remove inline emoji scripts & styles
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );
    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );

    // Admin customisation crap
    remove_action('plugins_loaded','wp_maybe_load_widgets',0);
    remove_action('plugins_loaded','wp_maybe_load_embeds',0);
    remove_action('plugins_loaded','wp_customize_include',0);
    remove_action('wp_loaded','_custom_header_background_just_in_time',100);
    remove_theme_support('custom-background');
    remove_theme_support('custom-header');

  }

  public function init_remove_DNS_prefetch() {
    add_filter('wp_resource_hints', array($this, 'remove_DNS_prefetch'), 10, 2 );
  }

  public function remove_DNS_prefetch( $hints, $relation_type ) {
   if ( 'dns-prefetch' === $relation_type ) {
      $matches = preg_grep('/emoji/', $hints);
      return array_diff( $hints, $matches );
    }
    return $hints;
  }

  public function init_remove_x_pingback() {
    add_filter('wp_headers', array($this, 'remove_x_pingback'), 10, 1 );
  }

  public function remove_x_pingback($headers) {
    unset($headers['X-Pingback']);
    return $headers;
  }


  public function init_redirect_feeds() {
    add_action('do_feed',               array($this, 'redirect_to_homepage'), 1);
    add_action('do_feed_rdf',           array($this, 'redirect_to_homepage'), 1);
    add_action('do_feed_rss',           array($this, 'redirect_to_homepage'), 1);
    add_action('do_feed_rss2',          array($this, 'redirect_to_homepage'), 1);
    add_action('do_feed_atom',          array($this, 'redirect_to_homepage'), 1);
    add_action('do_feed_rss2_comments', array($this, 'redirect_to_homepage'), 1);
    add_action('do_feed_atom_comments', array($this, 'redirect_to_homepage'), 1);
  }

  public static function redirect_to_homepage() {
    wp_redirect( home_url(), 301 );
  }

  public function init_force_lowercase_filenames() {
    add_filter('sanitize_file_name', array($this, 'force_lowercase_filenames'), 10, 1);
  }

  public function init_force_lowercase_urls() {
    add_action('init', array($this, 'force_lowercase_urls') );
  }

  public function force_lowercase_urls() {
    $url = $_SERVER['REQUEST_URI'];
    $params = '';
    if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['QUERY_STRING']; }
    if ( preg_match('/[\.]/', $url) ) { return; }
    if ( preg_match('/[A-Z]/', $url) ) {
      $lc_url = empty($params) ? strtolower($url) : strtolower(substr($url, 0, strrpos($url, '?'))) . '?' . $params;
      if ($lc_url !== $url) {
        wp_redirect( $lc_url, 301 );
      }
    }
  }

  public function force_lowercase_filenames($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    return strtolower($name) . $ext;
  }

  public function init_remove_dashboard_widgets() {
    add_action('wp_dashboard_setup', array($this, 'remove_dashboard_widgets') );
  }

  public function remove_dashboard_widgets() {
    remove_meta_box('dashboard_activity', 'dashboard', 'side' );
    remove_meta_box('dashboard_activity', 'dashboard', 'normal');
    remove_meta_box('dashboard_right_now', 'dashboard', 'normal' );
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal' );
    remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal' );
    remove_meta_box('dashboard_quick_press', 'dashboard', 'side' );
    remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side' );
  }

  public function init_remove_default_image_links() {
    add_action('admin_init', array($this, 'remove_default_image_links') );
  }

  public function init_disable_xmlrpc(){
    add_filter('xmlrpc_enabled', array($this, 'disable_xmlrpc') );
  }

  public function disable_xmlrpc() {
    return false;
  }

  public function remove_default_image_links() {
    $image_set = get_option( 'image_default_link_type' );
    if ($image_set !== 'none') {
      update_option('image_default_link_type', 'none');
    }
  }

}

$cleaner = new WP_Cleanup();
$cleaner->clean_cruft();
$cleaner->init_remove_DNS_prefetch();
$cleaner->init_remove_x_pingback();
$cleaner->init_redirect_feeds();
$cleaner->init_force_lowercase_filenames();
$cleaner->init_remove_dashboard_widgets();
$cleaner->init_force_lowercase_urls();
$cleaner->init_remove_default_image_links();
$cleaner->init_disable_xmlrpc();

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of