Skip to content

Instantly share code, notes, and snippets.

@richardtape
Created October 14, 2022 23:02
Show Gist options
  • Save richardtape/7dbf943ec0896dce6fa9e455d39b3d24 to your computer and use it in GitHub Desktop.
Save richardtape/7dbf943ec0896dce6fa9e455d39b3d24 to your computer and use it in GitHub Desktop.
WP Theme Clean Slate
<?php
// Add this to your functions.php for example...
add_action( 'init', 'rkn_clean_slate__init' );
/**
* WP has a lot of initial...err...stuff even with a completely blank theme. Let's remove quite a bit
* of that.
*
* @since 0.1.0
* @return void
*/
function rkn_clean_slate__init() {
// Remove Emoji.
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 EditURI as I don't use a 3rd-party editing tool.
remove_action ( 'wp_head', 'rsd_link' );
// Remove the manifest link.
remove_action( 'wp_head', 'wlwmanifest_link' );
// Remove the shortlink.
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
// Remove the Link header for the REST API.
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
// Remove oEmbed Discovery Links.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
// Remove WordPress.org Dns-prefetch.
remove_action( 'wp_head', 'wp_resource_hints', 2 );
// Remove WP Generator Tag.
remove_action( 'wp_head', 'wp_generator' );
// Remove default generated SkipLink. I'll have my own.
remove_action( 'wp_footer', 'the_block_template_skip_link' );
// disable comments feed.
add_filter( 'feed_links_show_comments_feed', '__return_false' );
// Remove meta robots tag.
remove_filter( 'wp_robots', 'wp_robots_max_image_preview_large' );
// Remove unwanted SVG filter injection WP.
remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
remove_action( 'wp_body_open', 'gutenberg_global_styles_render_svg_filters' ); // Gutenberg plugin o_O
// Remove default "global" styles.
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
remove_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles' ); // Gutenberg plugin o_O
// Remove Block Style Library.
add_action( 'wp_enqueue_scripts', 'rkn_remove_wp_block_library_css__wp_enqueue_scripts', 10 );
}//end rkn_clean_slate()
/**
* Specifically remove the CSS that WP enqueues for the block library as we'll be starting clean.
*
* @since 0.1.0
* @return void
*/
function rkn_remove_wp_block_library_css__wp_enqueue_scripts() {
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
}//end rkn_remove_wp_block_library_css__wp_enqueue_scripts()
@richardtape
Copy link
Author

(and yes, you absolutely should make sure you have a Skip Link in your site, but I think you're the one who should decide how and where that's implemented)

@colinhowells
Copy link

here's some i've gathered over the years in case you find them useful – i'll add the ones you have to this list as well:

// for wp-config.php
// define( 'CORE_UPGRADE_SKIP_NEW_BUNDLED', true );
// define('WP_POST_REVISIONS', false);
// OR
// define('WP_POST_REVISIONS', 5);

// https://chriswiegman.com/2021/04/creating-a-minimal-wordpress-theme-in-the-era-of-gutenberg/?mc_eid=c0038fb03d

// remove front-end js
function remove_core_js() {
	wp_deregister_script( 'wp-embed' );
}
add_action( 'wp_enqueue_scripts', 'remove_core_js' );


// remove block styles
function remove_block_styles() {
	wp_dequeue_style( 'wp-block-library' );
}
add_action( 'wp_enqueue_scripts', 'remove_block_styles' );


// no 'custom fields' in edit screen
add_filter('postmeta_form_keys', '__return_false');


// remove comments:

function remove_comment_js() {
	wp_dequeue_style( 'comment-reply' );
}
add_action( 'wp_enqueue_scripts', 'remove_comment_js' );

function remove_comments_in_admin_bar() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu( 'comments' );
}
add_action( 'wp_before_admin_bar_render', 'remove_comments_in_admin_bar' );

function remove_comments_in_admin_menu() {
	remove_menu_page( 'edit-comments.php' );
}
add_action( 'admin_menu', 'remove_comments_in_admin_menu' );

add_filter( 'feed_links_show_comments_feed', '__return_false' );


// prevent WP from listing all our users:

function remove_user_sitemap( WP_Sitemaps_Provider $provider, string $name ): WP_Sitemaps_Provider|boolean {
	return ( $name == 'users' ) ? false : $provider;
}
add_filter( 'wp_sitemaps_add_provider', 'remove_cuser_sitemap', 10, 2 );


// remove actions:

public function remove_actions() {
	remove_post_type_support( 'post', 'comments' );
	remove_post_type_support( 'page', 'comments' );
	remove_action('admin_init', '_maybe_update_core');
	remove_action('admin_init', '_maybe_update_plugins');
	remove_action('admin_init', '_maybe_update_themes');
	remove_action('admin_menu', '_add_themes_utility_last', 101);
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wlwmanifest_link');
	remove_action('wp_head', 'wp_generator');
	remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
	remove_action('wp_head', 'wp_shortlink_wp_head');
	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, 0);
	remove_action('template_redirect', 'rest_output_link_header', 11);
	remove_action('wp_print_styles', 'print_emoji_styles');
	remove_action('wp_head', 'print_emoji_detection_script', 7);
	remove_action('wp_head', 'wp_oembed_add_discovery_links');
	remove_action('wp_head', 'wp_oembed_add_host_js');
}
add_action( 'init', 'remove_actions' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment