Managing a WordPress site can sometimes feel overwhelming with all the extra features and functionalities that might not be necessary for everyone. The “Headache” snippet is a collection of code functions designed to remove or disable various WordPress elements that might be considered redundant or unnecessary for the average user. Below, we’ll go through each function in the snippet, explaining what it does and why it might be useful.
1. Disable Login Screen Language Switcher
<?php
add_filter('login_display_language_dropdown', '__return_false');
This function removes the language switcher dropdown from the login screen, simplifying the login interface.
2. Disable Useless Dashboard Widgets
<?php
function yourprefix_disable_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['normal']['core']['yoast_db_widget']);
unset($wp_meta_boxes['dashboard']['normal']['core']['wpseo-dashboard-overview']);
unset($wp_meta_boxes['dashboard']['normal']['core']['zerospam_dashboard_widget']);
unset($wp_meta_boxes['dashboard']['normal']['core']['simple_history_dashboard_widget']);
unset($wp_meta_boxes['dashboard']['normal']['core']['rg_forms_dashboard']);
}
add_action('wp_dashboard_setup', 'yourprefix_disable_dashboard_widgets', 999);
This function removes various widgets from the WordPress dashboard to reduce clutter and improve performance.
3. Disable Useless Admin Toolbar Nodes
<?php
function yourprefix_remove_toolbar_node($wp_admin_bar) {
$wp_admin_bar->remove_node('new-content');
$wp_admin_bar->remove_node('comments');
$wp_admin_bar->remove_node('search');
$wp_admin_bar->remove_node('wpseo-menu');
$wp_admin_bar->remove_node('duplicate-post');
$wp_admin_bar->remove_node('wp-logo');
$wp_admin_bar->remove_node('redis-cache');
}
add_action('admin_bar_menu', 'yourprefix_remove_toolbar_node', 999);
Removes unnecessary nodes (buttons and links) from the admin toolbar to streamline the interface.
4. Add Current Post Slug to Body Class
<?php
function yourprefix_add_slug_body_class($classes) {
global $post;
if (isset($post)) {
$classes[] = $post->post_type . '__' . $post->post_name;
}
return $classes;
}
add_filter('body_class', 'yourprefix_add_slug_body_class');
Adds the current post slug to the body class, which can be useful for custom CSS styling.
5. Remove Author Page URL
<?php
function yourprefix_author_page_redirect($link) {
$link = '';
return $link;
}
add_action('author_link', 'yourprefix_author_page_redirect');
Disables the author page link to enhance privacy and security.
6. Remove Comment Author URL
<?php
function yourprefix_remove_comment_author_link($return, $author, $comment_ID) {
return $author;
}
add_filter('get_comment_author_link', 'yourprefix_remove_comment_author_link', 10, 3);
Removes the URL link from comment author names to prevent spam and unwanted backlinks.
7. Remove Comment Website URL Field
<?php
function yourprefix_disable_comment_url($fields) {
unset($fields['url']);
return $fields;
}
function yourprefix_add_comment_url_filter() {
add_filter('comment_form_default_fields', 'yourprefix_disable_comment_url', 20);
}
add_action('after_setup_theme', 'yourprefix_add_comment_url_filter');
Removes the website URL field from the comment form to reduce spam.
8. Disable Emojis
<?php
function yourprefix_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', 'yourprefix_disable_emojis_tinymce');
add_filter('wp_resource_hints', 'yourprefix_disable_emojis_remove_dns_prefetch', 10, 2);
}
add_action('init', 'yourprefix_disable_emojis');
Disables the emoji functionality in WordPress to improve site performance and reduce unnecessary requests.
9. Disable Dashicons CSS for Non-Logged-In Users
<?php
function yourprefix_adminify_remove_dashicons() {
if (!is_admin_bar_showing() && !is_customize_preview()) {
wp_dequeue_style('dashicons');
wp_deregister_style('dashicons');
}
}
add_action('wp_print_styles', 'yourprefix_adminify_remove_dashicons', 100);
Removes the Dashicons stylesheet for users who are not logged in, improving page load times for visitors.
10. Disable XML-RPC
<?php
add_filter('xmlrpc_enabled', '__return_false');
add_filter('xmlrpc_methods', '__return_false');
Disables the XML-RPC API, which can be a security risk if not needed.
11. Disable jQuery Migrate
<?php
function yourprefix_remove_jquery_migrate($scripts) {
if (!is_admin() && isset($scripts->registered['jquery'])) {
$script = $scripts->registered['jquery'];
if ($script->deps) {
$script->deps = array_diff($script->deps, ['jquery-migrate']);
}
}
}
add_action('wp_default_scripts', 'yourprefix_remove_jquery_migrate');
Removes the jQuery Migrate script, which is often not needed and can reduce page load times.
12. Hide WordPress Version
<?php
function yourprefix_remove_version() {
return '';
}
add_filter('the_generator', 'yourprefix_remove_version');
remove_action('wp_head', 'wp_generator');
Hides the WordPress version from the HTML source code to improve security.
13. Remove Unnecessary Links from <head>
<?php
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_site_icon', 99);
remove_action('wp_head', 'wp_shortlink_wp_head', 10);
remove_action('template_redirect', 'wp_shortlink_header', 11);
Removes various unnecessary link tags from the <head> section of your site to reduce clutter and improve performance.
14. Disable RSS Feeds
<?php
function yourprefix_disable_feed() {
wp_die(__('No feed available, please visit the <a href="' . esc_url(home_url('/')) . '">homepage</a>!'));
}
add_action('do_feed', 'yourprefix_disable_feed', 1);
add_action('do_feed_rdf', 'yourprefix_disable_feed', 1);
add_action('do_feed_rss', 'yourprefix_disable_feed', 1);
add_action('do_feed_rss2', 'yourprefix_disable_feed', 1);
add_action('do_feed_atom', 'yourprefix_disable_feed', 1);
add_action('do_feed_rss2_comments', 'yourprefix_disable_feed', 1);
add_action('do_feed_atom_comments', 'yourprefix_disable_feed', 1);
Disables RSS feeds on your site, which can reduce server load and prevent content scraping.
15. Disable Comments
<?php
add_filter('comments_open', '__return_false');
Disables comments site-wide, removing the ability for users to leave comments.
16. Remove Meta Tags for DNS Prefetching
<?php
remove_action('wp_head', 'wp_resource_hints', 2);
Removes DNS prefetching for external resources, which can help improve privacy.
17. Remove Relational Links for Posts
<?php
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
Disables relational links for adjacent posts, which are often not needed.
18. Disable Self Pingbacks
<?php
function yourprefix_no_self_ping(&$links) {
$home = get_option('home');
foreach ($links as $l => $link) {
if (0 === strpos($link, $home)) {
unset($links[$l]);
}
}
}
add_action('pre_ping', 'yourprefix_no_self_ping');
Prevents your site from sending pingbacks to itself, reducing unnecessary server load.
19. Disable Default Users API Endpoints for Security
<?php
function yourprefix_disable_rest_endpoints(array $endpoints): array {
if (!is_user_logged_in()) {
if (isset($endpoints['/wp/v2/users'])) {
unset($endpoints['/wp/v2/users']);
}
if (isset($endpoints["/wp/v2/users/(?P<id>[\d]+)"])) {
unset($endpoints["/wp/v2/users/(?P<id>[\d]+)"]);
}
}
return $endpoints;
}
add_filter('rest_endpoints', 'yourprefix_disable_rest_endpoints');
Disables REST API endpoints related to users for non-logged-in users to enhance security.
20. Condition Access to REST API
<?php
add_filter('rest_authentication_errors', function ($errors) {
$route = $GLOBALS['wp']->query_vars['rest_route'];
if (str_contains($route, '/wp/v2/')) {
if (!is_user_logged_in()) {
if (!isset($_REQUEST['nonce'])) {
return new WP_Error(
'rest_not_logged_in',
__('Nonce parameter missing from query', 'mainsdejardin'),
['status' => 401]
);
} else {
if (!wp_verify_nonce($_REQUEST['nonce'], 'wp_rest')) {
return new WP_Error(
'rest_not_logged_in',
__('Incorrect nonce parameter in query', 'mainsdejardin'),
['status' => 401]
);
}
}
}
}
return $errors;
});
Adds an additional layer of security to the REST API by requiring a nonce parameter for certain routes.
21. Remove REST API Links
<?php
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('template_redirect', 'rest_output_link_header', 11);
Removes REST API links from the HTML <head> and HTTP headers to reduce potential security vulnerabilities.
22. Remove oEmbeds
<?php
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('wp_head', 'wp_oembed_add_host_js');
Disables oEmbed discovery links and JavaScript to improve performance.
23. Remove YOAST SEO HTML Comments
<?php
add_filter('wpseo_debug_markers', '__return_false');
Removes the HTML comments added by YOAST SEO for a cleaner source code.
24. Disable Color Scheme Picker from User Dashboard
<?php
remove_action('admin_color_scheme_picker', 'admin_color_scheme_picker');
Disables the color scheme picker in the user profile to simplify the dashboard.
25. Remove JPEG Compression
<?php
function yourprefix_remove_jpeg_compression() {
return 100;
}
add_filter('jpeg_quality', 'yourprefix_remove_jpeg_compression', 10, 2);
Sets JPEG image compression to 100%, ensuring the highest possible quality.
26. Remove Classic Theme Styles
<?php
function yourprefix_remove_classic_theme_styles() {
wp_dequeue_style('classic-theme-styles');
}
add_action('wp_enqueue_scripts', 'yourprefix_remove_classic_theme_styles');
Removes the classic theme styles to reduce unnecessary CSS loading.
27. Remove Query String from Static Resources
<?php
function yourprefix_remove_script_version(string $url): string {
if (is_admin()) {
return $url;
}
if ($url) {
return esc_url(remove_query_arg('ver', $url));
}
return $url;
}
add_filter('script_loader_src', 'yourprefix_remove_script_version', 15, 1);
add_filter('style_loader_src', 'yourprefix_remove_script_version', 15, 1);
Removes the version query string from static resources to improve caching.
28. Disable Attachment Template Loading and Redirect to 404
<?php
function yourprefix_attachment_redirect_not_found() {
if (is_attachment()) {
global $wp_query;
$wp_query->set_404();
status_header(404);
}
}
add_filter('template_redirect', 'yourprefix_attachment_redirect_not_found');
Redirects attachment pages to a 404 error page, preventing access to standalone attachment pages.
29. Disable Attachment Canonical Redirect Links
<?php
function yourprefix_disable_attachment_canonical_redirect_url($url) {
yourprefix_attachment_redirect_not_found();
return $url;
}
add_filter('redirect_canonical', 'yourprefix_disable_attachment_canonical_redirect_url', 0, 2);
Disables canonical redirect URLs for attachment pages, which complements the previous function by ensuring attachment links are properly handled.
30. Disable Attachment Links
<?php
function yourprefix_disable_attachment_link($url, $id) {
if ($attachment_url = wp_get_attachment_url($id)) {
return $attachment_url;
}
return $url;
}
add_filter('attachment_link', 'yourprefix_disable_attachment_link', 10, 2);
Removes the link from attachment URLs to prevent users from navigating to attachment pages.
31. Discourage Search Engines from Indexing in Non-Production Environments
<?php
function yourprefix_disable_indexing() {
return wp_get_environment_type() === 'production' ? true : 0;
}
add_action('pre_option_blog_public', 'yourprefix_disable_indexing');
Automatically discourages search engines from indexing the site when it is not in a production environment, helping to prevent accidental indexing of development or staging sites.
Full Snippet
Here is the complete “Headache” snippet for you to copy and paste into your WordPress theme’s functions.php file or a custom plugin:
<?php
/*
* ================================ Headache
* Remove a bunch of things that are useless for the common humans
* who are using WordPress
*/
// Disable login screen language switcher
add_filter('login_display_language_dropdown', '__return_false');
// Disable useless dashboard widgets
function yourprefix_disable_dashboard_widgets() {
global $wp_meta_boxes;
// wp..
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
// plugins
unset($wp_meta_boxes['dashboard']['normal']['core']['yoast_db_widget']);
unset($wp_meta_boxes['dashboard']['normal']['core']['wpseo-dashboard-overview']);
unset($wp_meta_boxes['dashboard']['normal']['core']['zerospam_dashboard_widget']);
unset($wp_meta_boxes['dashboard']['normal']['core']['simple_history_dashboard_widget']);
unset($wp_meta_boxes['dashboard']['normal']['core']['rg_forms_dashboard']);
}
add_action('wp_dashboard_setup', 'yourprefix_disable_dashboard_widgets', 999);
// Disable useless admin toolbar node
function yourprefix_remove_toolbar_node($wp_admin_bar) {
$wp_admin_bar->remove_node('new-content');
$wp_admin_bar->remove_node('comments');
$wp_admin_bar->remove_node('search');
$wp_admin_bar->remove_node('wpseo-menu');
$wp_admin_bar->remove_node('duplicate-post');
$wp_admin_bar->remove_node('wp-logo');
$wp_admin_bar->remove_node('redis-cache');
}
add_action('admin_bar_menu', 'yourprefix_remove_toolbar_node', 999);
// Add current post slug to body class
function yourprefix_add_slug_body_class($classes) {
global $post;
if (isset($post)) {
$classes[] = $post->post_type . '__' . $post->post_name;
}
return $classes;
}
add_filter('body_class', 'yourprefix_add_slug_body_class');
// Remove author page URL
function yourprefix_author_page_redirect($link) {
$link = '';
return $link;
}
add_action('author_link', 'yourprefix_author_page_redirect');
// Remove comment author URL
function yourprefix_remove_comment_author_link($return, $author, $comment_ID) {
return $author;
}
add_filter('get_comment_author_link', 'yourprefix_remove_comment_author_link', 10, 3);
// Remove comment website URL
function yourprefix_disable_comment_url($fields) {
unset($fields['url']);
return $fields;
}
function yourprefix_add_comment_url_filter() {
add_filter('comment_form_default_fields', 'yourprefix_disable_comment_url', 20);
}
add_action('after_setup_theme', 'yourprefix_add_comment_url_filter');
// Disable emoji's
//Filter function used to remove the tinymce emoji plugin.
function yourprefix_disable_emojis_tinymce($plugins) {
if (is_array($plugins)) {
return array_diff($plugins, ['wpemoji']);
} else {
return [];
}
}
//Remove emoji CDN hostname from DNS prefetching hints.
function yourprefix_disable_emojis_remove_dns_prefetch($urls, $relation_type) {
if ('dns-prefetch' == $relation_type) {
$emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/');
$urls = array_diff($urls, [$emoji_svg_url]);
}
return $urls;
}
function yourprefix_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', 'yourprefix_disable_emojis_tinymce');
add_filter('wp_resource_hints', 'yourprefix_disable_emojis_remove_dns_prefetch', 10, 2);
}
add_action('init', 'yourprefix_disable_emojis');
// Disable Dashicons CSS for non-logged-in users
// Remove Dashicons from Admin Bar for non logged in users
function yourprefix_adminify_remove_dashicons() {
if (!is_admin_bar_showing() && !is_customize_preview()) {
wp_dequeue_style('dashicons');
wp_deregister_style('dashicons');
}
}
add_action('wp_print_styles', 'yourprefix_adminify_remove_dashicons', 100);
// Disable XML RPC
add_filter('xmlrpc_enabled', '__return_false');
add_filter('xmlrpc_methods', '__return_false');
// Disable jQuery migrate
function yourprefix_remove_jquery_migrate($scripts) {
if (!is_admin() && isset($scripts->registered['jquery'])) {
$script = $scripts->registered['jquery'];
if ($script->deps) {
// Check whether the script has any dependencies
$script->deps = array_diff($script->deps, ['jquery-migrate']);
}
}
}
add_action('wp_default_scripts', 'yourprefix_remove_jquery_migrate');
// Hide WP version
function yourprefix_remove_version() {
return '';
}
add_filter('the_generator', 'yourprefix_remove_version');
remove_action('wp_head', 'wp_generator');
// Remove wlwmanifest Link
remove_action('wp_head', 'wlwmanifest_link');
// Remove RSD Link
remove_action('wp_head', 'rsd_link');
// Remove generated icons.
remove_action('wp_head', 'wp_site_icon', 99);
// Remove shortlink tag from <head>.
remove_action('wp_head', 'wp_shortlink_wp_head', 10);
// Remove Shortlink
remove_action('template_redirect', 'wp_shortlink_header', 11);
// Disable RSS Feeds
function yourprefix_disable_feed() {
wp_die(__('No feed available, please visit the <a href="' . esc_url(home_url('/')) . '">homepage</a>!'));
}
add_action('do_feed', 'yourprefix_disable_feed', 1);
add_action('do_feed_rdf', 'yourprefix_disable_feed', 1);
add_action('do_feed_rss', 'yourprefix_disable_feed', 1);
add_action('do_feed_rss2', 'yourprefix_disable_feed', 1);
add_action('do_feed_atom', 'yourprefix_disable_feed', 1);
add_action('do_feed_rss2_comments', 'yourprefix_disable_feed', 1);
add_action('do_feed_atom_comments', 'yourprefix_disable_feed', 1);
// Remove RSS Feed links
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'feed_links', 2);
// Disable comments.
add_filter('comments_open', '__return_false');
// Remove meta rel=dns-prefetch href=//s.w.org
remove_action('wp_head', 'wp_resource_hints', 2);
// Remove relational links for the posts.
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
// Disable Self Pingbacks
function yourprefix_no_self_ping(&$links) {
$home = get_option('home');
foreach ($links as $l => $link) {
if (0 === strpos($link, $home)) {
unset($links[$l]);
}
}
}
add_action('pre_ping', 'yourprefix_no_self_ping');
// Disable default users API endpoints for security.
// https://www.wp-tweaks.com/hackers-can-find-your-wordpress-username/
function yourprefix_disable_rest_endpoints(array $endpoints): array {
if (!is_user_logged_in()) {
if (isset($endpoints['/wp/v2/users'])) {
unset($endpoints['/wp/v2/users']);
}
if (isset($endpoints["/wp/v2/users/(?P<id>[\d]+)"])) {
unset($endpoints["/wp/v2/users/(?P<id>[\d]+)"]);
}
}
return $endpoints;
}
add_filter('rest_endpoints', 'yourprefix_disable_rest_endpoints');
// Condition access to REST API
add_filter('rest_authentication_errors', function ($errors) {
$route = $GLOBALS['wp']->query_vars['rest_route'];
if (str_contains($route, '/wp/v2/')) {
if (!is_user_logged_in()) {
if (!isset($_REQUEST['nonce'])) {
return new WP_Error(
'rest_not_logged_in',
__('Nonce parameter missing from query', 'mainsdejardin'),
['status' => 401]
);
} else {
if (!wp_verify_nonce($_REQUEST['nonce'], 'wp_rest')) {
return new WP_Error(
'rest_not_logged_in',
__('Incorrect nonce parameter in query', 'mainsdejardin'),
['status' => 401]
);
}
}
}
}
return $errors;
});
// Remove REST API links
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('template_redirect', 'rest_output_link_header', 11);
// Remove oEmbeds.
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('wp_head', 'wp_oembed_add_host_js');
// Remove YOAST SEO HTML comments
add_filter('wpseo_debug_markers', '__return_false');
// Disable color scheme from user dashboard
remove_action('admin_color_scheme_picker', 'admin_color_scheme_picker');
// Remove JPEG compression.
function yourprefix_remove_jpeg_compression() {
return 100;
}
add_filter('jpeg_quality', 'yourprefix_remove_jpeg_compression', 10, 2);
// Remove classic theme styles.
// https://github.com/WordPress/WordPress/commit/143fd4c1f71fe7d5f6bd7b64c491d9644d861355
function yourprefix_remove_classic_theme_styles() {
wp_dequeue_style('classic-theme-styles');
}
add_action('wp_enqueue_scripts', 'yourprefix_remove_classic_theme_styles');
// Remove ?ver= query from styles and scripts.
function yourprefix_remove_script_version(string $url): string {
if (is_admin()) {
return $url;
}
if ($url) {
return esc_url(remove_query_arg('ver', $url));
}
return $url;
}
add_filter('script_loader_src', 'yourprefix_remove_script_version', 15, 1);
add_filter('style_loader_src', 'yourprefix_remove_script_version', 15, 1);
// Disable attachment template loading and redirect to 404.
function yourprefix_attachment_redirect_not_found() {
if (is_attachment()) {
global $wp_query;
$wp_query->set_404();
status_header(404);
}
}
add_filter('template_redirect', 'yourprefix_attachment_redirect_not_found');
// Disable attachment canonical redirect links.
function yourprefix_disable_attachment_canonical_redirect_url($url) {
yourprefix_attachment_redirect_not_found();
return $url;
}
add_filter('redirect_canonical', 'yourprefix_disable_attachment_canonical_redirect_url', 0, 2);
// Disable attachment links.
function yourprefix_disable_attachment_link($url, $id) {
if ($attachment_url = wp_get_attachment_url($id)) {
return $attachment_url;
}
return $url;
}
add_filter('attachment_link', 'yourprefix_disable_attachment_link', 10, 2);
// Discourage search engines from indexing in non-production environments.
function yourprefix_disable_indexing() {
return wp_get_environment_type() === 'production' ? true : 0;
}
add_action('pre_option_blog_public', 'yourprefix_disable_indexing');
/*
* ================================ END Headache
*/
Changing the Prefix
Using a unique prefix for your functions helps prevent conflicts with other plugins or themes. To change the prefix:
- Decide on a new prefix, such as
mycustomprefix_. - Replace all instances of
yourprefix_with your new prefix using a find-and-replace tool in your code editor.
For example:
function mycustomprefix_disable_dashboard_widgets() {
// Function code...
}
add_action('wp_dashboard_setup', 'mycustomprefix_disable_dashboard_widgets', 999);
By following these instructions, you can ensure that your custom functions remain unique and do not interfere with other parts of your WordPress setup. Happy coding!

Leave a Reply