Although WordPress is really powerful CMS there are some annoying things I would like to be deactivated by default. For example, I don’t see the reason why commenting should be enabled by default. Therefore, today I’m going to show you 7 best WordPressi tricks I‘m using on every site.
Before we start you need to know that you need to add all the code snippets here below either to your child theme’s functions.php file or better yet, inside Code Snippets plugin code box.
Disable WordPress comments without a plugin
Instead of using a Disable Comments plugin (or similar) you can get rid of the comment area by using this snippet here.
// Disable comments
function __disable_feature($data) { return false; }
add_filter('comments_number', '__disable_feature');
add_filter('comments_open', '__disable_feature');
Remove Emojis
Why would one want to do so? Well, take me for example. I haven’t use any emojis on my sites for years and I’m not planning to use them either. But, since they are activated by default, then every time you load a page an emoji script is loaded. Therefore, I’m removing emoji script to reduce the page load size and requests.
// Remove Emojis
add_action( 'init', 'generate_disable_wp_emojicons' );
function generate_disable_wp_emojicons()
{
// all actions related to emojis
remove_action( 'admin_print_styles', 'print_emoji_styles' );
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_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
}
Unregister unnecessary WordPress widgets
WordPress Widgets area is loaded with all sorts of widgets 95% users won’t need. Take Archive or Calendar widgets for example. So, with this little code snippet here below you can remove these widgets you don’t need and unclutter your widgets area.
// Unregister widgets
function unregister_default_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Media_Audio');
unregister_widget('WP_Widget_Media_Video');
unregister_widget( 'WP_Widget_Tag_Cloud' );
}
add_action('widgets_init', 'unregister_default_widgets', 11);
Remove WordPress dashboard widgets
Now let’s modify a WordPress dashboard which is also full of unnecessary widgets. Yes, you can turn then off under Screen options by I like to deactivate them for all users. So, grab this code and remove WordPress dashboard widgets you don’t need.
function remove_widgets() {
remove_meta_box( 'dashboard_primary','dashboard','side' ); // WordPress.com Blog
remove_meta_box( 'dashboard_plugins','dashboard','normal' ); // Plugins
remove_meta_box( 'dashboard_right_now','dashboard', 'normal' ); // Right Now
remove_action( 'welcome_panel','wp_welcome_panel' ); // Welcome Panel
remove_action( 'try_gutenberg_panel', 'wp_try_gutenberg_panel'); // Try Gutenberg
remove_meta_box('dashboard_quick_press','dashboard','side'); // Quick Press widget
remove_meta_box('dashboard_recent_drafts','dashboard','side'); // Recent Drafts
remove_meta_box('dashboard_secondary','dashboard','side'); // Other WordPress News
remove_meta_box('dashboard_incoming_links','dashboard','normal'); //Incoming Links
remove_meta_box('rg_forms_dashboard','dashboard','normal'); // Gravity Forms
remove_meta_box('dashboard_recent_comments','dashboard','normal'); // Recent Comments
remove_meta_box('icl_dashboard_widget','dashboard','normal'); // Multi Language Plugin
remove_meta_box('dashboard_activity','dashboard', 'normal'); // Activity
remove_meta_box('dashboard_site_health', 'dashboard', 'normal'); // Site health
remove_meta_box( 'e-dashboard-overview', 'dashboard', 'normal'); // Elementor
}
add_action( 'wp_dashboard_setup', 'remove_widgets' );
Add file size column to your WordPress Media list table
As the heading says, this snippet here below adds a file size column so you can see how big are the uploaded files without the need to open every file one at a time.
add_filter('manage_media_columns','sk_media_columns_filesize');functionsk_media_columns_filesize($posts_columns){$posts_columns['filesize'] =__('File size','my-theme-text-domain');return$posts_columns;}add_action('manage_media_custom_column','sk_media_custom_column_filesize',10,2);functionsk_media_custom_column_filesize($column_name,$post_id){if('filesize'!==$column_name){return;}$bytes =filesize(get_attached_file($post_id));echo size_format($bytes,2);}add_action('admin_print_styles-upload.php','sk_filesize_column_filesize');functionsk_filesize_column_filesize(){echo'<style>.fixed .column-filesize{width:10%;}</style>';}
Add menu for WordPress Reusable Blocks
If you’re using Reusable Blocks then you need a shortcut to them. Here is the code snippet which adds a Reusable Blocks menu to the WordPress admin sidebar.
functionreusable_blocks_menu(){add_menu_page('Reusable Blocks','Reusable Blocks','edit_posts','edit.php?post_type=wp_block','','dashicons-editor-table',10);}add_action('admin_menu','reusable_blocks_menu');
Disable WordPress auto-update email notifications for plugins and themes
Since WordPress 5.5 there is a nifty feature which allows you to activate auto-updates for themes and plugins. If you do so then you will be surprised about how many “Your plugin is updated”email you receive weekly. Therefore,If this annoys you lik it annoys me,then use this snippet here below to deactivate those emails.
// Disable auto-update email notifications for plugins.add_filter('auto_plugin_update_send_email','__return_false');// Disable auto-update email notifications for themes.add_filter('auto_theme_update_send_email','__return_false');
Bonus tip:Leverage Browser caching and enable GZIp compression
## LEVERAGEBROWSERCACHING##Header unset PragmaFileETag NoneHeader unset ETag## EXPIRESCACHING##<IfModulemod_expires.c>ExpiresActiveOnExpiresByTypeimage/jpg"access 1 month"ExpiresByTypeimage/jpeg"access 1 month"ExpiresByTypeimage/gif"access 1 month"ExpiresByTypeimage/png"access 1 month"ExpiresByTypetext/css"access 1 week"ExpiresByTypetext/html"access 0 seconds"ExpiresByTypetext/xml"access 0 secondsExpiresByType text/json "access0secondsExpiresByTypeapplication/pdf"access 1 month"ExpiresByTypetext/x-javascript"access 1 month"ExpiresByTypeapplication/x-shockwave-flash"access 1 month"ExpiresByTypeimage/x-icon"access 1 year"ExpiresDefault"access 1 month"</IfModule>##ENDLEVERAGEBROWSERCACHING###GZIPCOMPRESSION<IfModulemod_deflate.c>#CompressHTML,CSS,JavaScript,Text,XMLandfontsAddOutputFilterByTypeDEFLATEapplication/javascriptAddOutputFilterByTypeDEFLATEapplication/rss+xmlAddOutputFilterByTypeDEFLATEapplication/vnd.ms-fontobjectAddOutputFilterByTypeDEFLATEapplication/x-fontAddOutputFilterByTypeDEFLATEapplication/x-font-opentypeAddOutputFilterByTypeDEFLATEapplication/x-font-otfAddOutputFilterByTypeDEFLATEapplication/x-font-truetypeAddOutputFilterByTypeDEFLATEapplication/x-font-ttfAddOutputFilterByTypeDEFLATEapplication/x-javascriptAddOutputFilterByTypeDEFLATEapplication/xhtml+xmlAddOutputFilterByTypeDEFLATEapplication/xmlAddOutputFilterByTypeDEFLATEfont/opentypeAddOutputFilterByTypeDEFLATEfont/otfAddOutputFilterByTypeDEFLATEfont/ttfAddOutputFilterByTypeDEFLATEimage/svg+xmlAddOutputFilterByTypeDEFLATEimage/x-iconAddOutputFilterByTypeDEFLATEtext/cssAddOutputFilterByTypeDEFLATEtext/htmlAddOutputFilterByTypeDEFLATEtext/javascriptAddOutputFilterByTypeDEFLATEtext/plainAddOutputFilterByTypeDEFLATEtext/xml#Removebrowserbugs(onlyneededforreallyoldbrowsers)BrowserMatch^Mozilla/4gzip-only-text/htmlBrowserMatch^Mozilla/4\.0[678]no-gzipBrowserMatch\bMSIE!no-gzip!gzip-only-text/htmlHeaderappendVaryUser-Agent</IfModule>#ENDGZIPCOMPRESSION