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.
/**
* 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
'