Skip to content

Instantly share code, notes, and snippets.

@elicohenator
Created May 20, 2021 06:47
Show Gist options
  • Save elicohenator/c61f36fee13f486b151ddab0f50db165 to your computer and use it in GitHub Desktop.
Save elicohenator/c61f36fee13f486b151ddab0f50db165 to your computer and use it in GitHub Desktop.
WordPress Cleanup functions
<?php
/**
* Cleanup version 2021.5.20
* save to your theme and use from functions.php:
* require get_template_directory() . '/helpers/cleanup.php';
*/
/**
* Disable the emoji's
*/
function cleanup_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');
// Remove from TinyMCE
add_filter('tiny_mce_plugins', 'disable_emojis_tinymce');
}
add_action('init', 'cleanup_disable_emojis');
/**
* Filter out the tinymce emoji plugin.
*/
function disable_emojis_tinymce($plugins)
{
if (is_array($plugins)) {
return array_diff($plugins, array('wpemoji'));
} else {
return array();
}
}
/**
* Clean WP version
*/
function cleanup_remove_version()
{
return '';
}
add_filter('the_generator', 'cleanup_remove_version');
/**
* Remove RSD link, wlw manifest, wp_shorlink, oEmbed discovery links, rest output from wp_head
*/
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_oembed_add_discovery_links', 10);
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
/**
* Limit WP Post revisions to 3
*/
if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 3);
/**
* Disbale comments.
*/
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment