Last active
October 30, 2020 21:14
-
-
Save alanzhaonys/b69cf996ae6cdbfbd386a745da2b3bd6 to your computer and use it in GitHub Desktop.
WordPress Admin Bar, Dashboard, head and footer Clean Up
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 | |
// | |
// | |
// Below is the code that cleans up dashboard and admin bar | |
// | |
// | |
// | |
// Remove admin bar from frontend | |
// | |
add_filter('show_admin_bar', '__return_false'); | |
// | |
// Clean up dashboard | |
// | |
function disable_default_dashboard_widgets() | |
{ | |
// WordPress dashboards | |
remove_meta_box('dashboard_right_now', 'dashboard', 'core'); | |
//remove_meta_box('dashboard_activity', 'dashboard', 'core'); | |
remove_meta_box('dashboard_recent_comments', 'dashboard', 'core'); | |
remove_meta_box('dashboard_incoming_links', 'dashboard', 'core'); | |
remove_meta_box('dashboard_plugins', 'dashboard', 'core'); | |
remove_meta_box('dashboard_quick_press', 'dashboard', 'core'); | |
remove_meta_box('dashboard_recent_drafts', 'dashboard', 'core'); | |
remove_meta_box('dashboard_primary', 'dashboard', 'core'); | |
remove_meta_box('dashboard_secondary', 'dashboard', 'core'); | |
// Yoast SEO dashboard | |
remove_meta_box('wpseo-dashboard-overview', 'dashboard', 'side'); | |
// Gravity form dashboard | |
remove_meta_box('rg_forms_dashboard', 'dashboard', 'core'); | |
// Wordfence dashboard | |
remove_meta_box('wordfence_activity_report_widget', 'dashboard', 'core'); | |
} | |
add_action('admin_init', 'disable_default_dashboard_widgets'); | |
// This just won't go away like the others | |
function disable_some_widgets_wont_go_away() | |
{ | |
// Redux framework | |
remove_meta_box('redux_dashboard_widget', 'dashboard', 'side'); | |
} | |
add_action('admin_bar_menu', 'disable_some_widgets_wont_go_away'); | |
// | |
// Remove welcome panel | |
// | |
remove_action('welcome_panel', 'wp_welcome_panel'); | |
// | |
// Remove Yoast SEO link from admin bar | |
// | |
function seo_admin_bar() | |
{ | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('wpseo-menu'); | |
} | |
add_action('wp_before_admin_bar_render', 'seo_admin_bar', 999); | |
function gf_admin_bar() | |
{ | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('gform-forms'); | |
} | |
add_action('wp_before_admin_bar_render', 'gf_admin_bar', 999); | |
// | |
// Remove admin bar logo | |
// | |
function remove_wp_logo($wp_admin_bar) | |
{ | |
$wp_admin_bar->remove_node('wp-logo'); | |
} | |
add_action('admin_bar_menu', 'remove_wp_logo', 999); | |
// | |
// Remove comment from admin bar | |
// | |
function remove_comment_from_admin_bar() | |
{ | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('comments'); | |
} | |
add_action('wp_before_admin_bar_render', 'remove_comment_from_admin_bar'); | |
// | |
// Remove comment from admin menu | |
// | |
function remove_comment_from_admin_menus() | |
{ | |
remove_menu_page('edit-comments.php'); | |
} | |
add_action('admin_menu', 'remove_comment_from_admin_menus'); | |
// | |
// Remove comment support from post and pages | |
// | |
function remove_comment_support() | |
{ | |
remove_post_type_support('post', 'comments'); | |
remove_post_type_support('page', 'comments'); | |
} | |
add_action('init', 'remove_comment_support', 100); | |
// | |
// | |
// Below is the to clean up the head/footer. | |
// I also could stop using wp_head() but future plugins and methods that injects anything to head/footer will cease to work. | |
// | |
// | |
// | |
// Disable the emoji's | |
// https://kinsta.com/knowledgebase/disable-emojis-wordpress/#disable-emojis-code | |
// | |
function disable_emojis() | |
{ | |
remove_action('wp_head', 'print_emoji_detection_script', 7); | |
remove_action('admin_print_scripts', 'print_emoji_detection_script'); | |
remove_action('wp_print_styles', 'print_emoji_styles'); | |
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'); | |
add_filter('tiny_mce_plugins', 'disable_emojis_tinymce'); | |
add_filter('wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2); | |
} | |
add_action('init', 'disable_emojis'); | |
/** | |
* Filter function used to remove the tinymce emoji plugin. | |
* | |
* @param array $plugins | |
* @return array Difference betwen the two arrays | |
*/ | |
function disable_emojis_tinymce($plugins) | |
{ | |
if (is_array($plugins)) { | |
return array_diff($plugins, array('wpemoji')); | |
} else { | |
return array(); | |
} | |
} | |
/** | |
* Remove emoji CDN hostname from DNS prefetching hints. | |
* | |
* @param array $urls URLs to print for resource hints. | |
* @param string $relation_type The relation type the URLs are printed for. | |
* @return array Difference betwen the two arrays. | |
*/ | |
function disable_emojis_remove_dns_prefetch($urls, $relation_type) | |
{ | |
if ('dns-prefetch' == $relation_type) { | |
/** This filter is documented in wp-includes/formatting.php */ | |
$emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/'); | |
$urls = array_diff($urls, array($emoji_svg_url)); | |
} | |
return $urls; | |
} | |
// | |
// Disable embeds | |
// https://kinsta.com/knowledgebase/disable-embeds-wordpress/#disable-embeds-code | |
// | |
function disable_embeds_code_init() | |
{ | |
// Remove the REST API endpoint. | |
remove_action('rest_api_init', 'wp_oembed_register_route'); | |
// Turn off oEmbed auto discovery. | |
add_filter('embed_oembed_discover', '__return_false'); | |
// Don't filter oEmbed results. | |
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); | |
// Remove oEmbed discovery links. | |
remove_action('wp_head', 'wp_oembed_add_discovery_links'); | |
// Remove oEmbed-specific JavaScript from the front-end and back-end. | |
remove_action('wp_head', 'wp_oembed_add_host_js'); | |
add_filter('tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin'); | |
// Remove all embeds rewrite rules. | |
add_filter('rewrite_rules_array', 'disable_embeds_rewrites'); | |
// Remove filter of the oEmbed result before any HTTP requests are made. | |
remove_filter('pre_oembed_result', 'wp_filter_pre_oembed_result', 10); | |
} | |
add_action('init', 'disable_embeds_code_init', 9999); | |
function disable_embeds_tiny_mce_plugin($plugins) | |
{ | |
return array_diff($plugins, array('wpembed')); | |
} | |
function disable_embeds_rewrites($rules) | |
{ | |
foreach ($rules as $rule => $rewrite) { | |
if (false !== strpos($rewrite, 'embed=true')) { | |
unset($rules[$rule]); | |
} | |
} | |
return $rules; | |
} | |
// | |
// Remove junk from head | |
// | |
remove_action('wp_head', 'rsd_link'); | |
remove_action('wp_head', 'wp_generator'); | |
remove_action('wp_head', 'rest_output_link_wp_head'); | |
remove_action('wp_head', 'wlwmanifest_link'); | |
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); | |
remove_action('wp_head', 'wp_resource_hints', 2); | |
// | |
// Remove Gutenberg Block Library CSS from loading on the frontend | |
// | |
function smartwp_remove_wp_block_library_css() | |
{ | |
wp_dequeue_style('wp-block-library'); | |
wp_dequeue_style('wp-block-library-theme'); | |
wp_dequeue_style('wc-block-style'); | |
} | |
add_action('wp_enqueue_scripts', 'smartwp_remove_wp_block_library_css', 100); | |
// | |
// Remove Yoast comments and other junk | |
// | |
add_filter('wpseo_debug_markers', '__return_false'); | |
function disable_yoast_schema_data($data) | |
{ | |
$data = array(); | |
return $data; | |
} | |
add_filter('wpseo_json_ld_output', 'disable_yoast_schema_data', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment