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' );
/**
* Filter the Media list table columns to add a File Size column.
*
* @param array $posts_columns Existing array of columns displayed in the Media list table.
* @return array Amended array of columns to be displayed in the Media list table.
*/
function sk_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 );
/**
* Display File Size custom column in the Media list table.
*
* @param string $column_name Name of the custom column.
* @param int $post_id Current Attachment ID.
*/
function sk_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' );
/**
* Adjust File Size column on Media Library page in WP admin
*/
function sk_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.
function reusable_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
## LEVERAGE BROWSER CACHING ##
Header unset Pragma
FileETag None
Header unset ETag
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType text/html "access 0 seconds"
ExpiresByType text/xml "access 0 seconds
ExpiresByType text/json "access 0 seconds
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## END LEVERAGE BROWSER CACHING ##
# GZIP COMPRESSION
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
# END GZIP COMPRESSION