No doubt WordPress is one of the most powerful CMS around and a few annoying things also are part of it.
Like a few of the things should be deactivated by default. Why commenting is enabled by default? Or who uses emoji?
Find below tricks while you setup a WordPress or even managing one.
Tricks we’re using on every site powered by Cloudmatic.
You need to add the code snippets either to your theme’s functions.php file or update wordpress .htaccess file.
Let go the Commenting system
No need to install another plugin and burden your wordpress to disable Comments. Get rid of the comment 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’);
Kick those never used Emojis
We do not use any emojis on our sites.
They are activated by default while wordpress is installed.
And every time you load a page an emoji script is loaded reducing the site’s performance.
// 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’ );
}
A few WordPress widgets never used
Most of the widgets are not used in majoroty of the wordpress websites.
Links or Archive or Media Video or Calendar widgets can be remove to unclutter your wordpress 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);
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
‘
‘;
}
Leverage Browser caching and enable GZIp compression
## LEVERAGE BROWSER CACHING ##
Header unset Pragma
FileETag None
Header unset ETag
## EXPIRES CACHING ##
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”
## END LEVERAGE BROWSER CACHING ##
# GZIP COMPRESSION
# 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
# END GZIP COMPRESSION
How to remove Woocommerce checkout fields without a plugin?
Here below are all the fields you can remove.
// Remove checkout fields
add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );
function custom_override_checkout_fields( $fields ) {
// remove billing fields
unset($fields[‘billing’][‘billing_first_name’]); // Billing First name
unset($fields[‘billing’][‘billing_last_name’]); // Billing Last name
unset($fields[‘billing’][‘billing_company’]); // Billing company
unset($fields[‘billing’][‘billing_address_1’]); // Billing Address 1
unset($fields[‘billing’][‘billing_address_2’]); // Billing Address 2
unset($fields[‘billing’][‘billing_city’]); // Billing city
unset($fields[‘billing’][‘billing_postcode’]); // Billing postcode
unset($fields[‘billing’][‘billing_country’]); // Billing country
unset($fields[‘billing’][‘billing_state’]); // Billing state
unset($fields[‘billing’][‘billing_phone’]); // Billing phone
unset($fields[‘billing’][‘billing_email’]); // Billing email
// remove shipping fields
unset($fields[‘shipping’][‘shipping_first_name’]); // Shipping first name
unset($fields[‘shipping’][‘shipping_last_name’]); // Shipping last name
unset($fields[‘shipping’][‘shipping_company’]); // Shipping company
unset($fields[‘shipping’][‘shipping_address_1’]); // Shipping address 1
unset($fields[‘shipping’][‘shipping_address_2’]); // Shipping address 2
unset($fields[‘shipping’][‘shipping_city’]); // Shipping city
unset($fields[‘shipping’][‘shipping_postcode’]); // Shipping postcode
unset($fields[‘shipping’][‘shipping_country’]); // Shipping country
unset($fields[‘shipping’][‘shipping_state’]); // Shipping state
// remove order comment fields
unset($fields[‘order’][‘order_comments’]); // Order comments
return $fields;
}
You don’t have to paste all of them, instead use only these you need. For example, if you need to remove billing address 2, state and company fields then use this code here below.
// Remove checkout fields
add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );
function custom_override_checkout_fields( $fields ) {
// remove billing fields
unset($fields[‘billing’][‘billing_company’]); // Billing company
unset($fields[‘billing’][‘billing_address_2’]); // Billing Address 2
unset($fields[‘billing’][‘billing_state’]); // Billing state
return $fields;
}
How to make Woocommerce checkout fields optional?
Pay attention that “true” =required and if you want the field to be optional then set it to “false”.
add_filter( ‘woocommerce_default_address_fields’ , ‘optional_default_address_fields’ );
function optional_default_address_fields( $address_fields ) {
$address_fields[‘first_name’][‘required’] = true;
$address_fields[‘last_name’][‘required’] = true;
$address_fields[‘company’][‘required’] = false;
$address_fields[‘address_1’][‘required’] = true;
$address_fields[‘address_2’][‘required’] = false;
$address_fields[‘country’][‘required’] = false;
$address_fields[‘postcode’][‘required’] = false;
$address_fields[‘city’][‘required’] = false;
$address_fields[‘state’][‘required’] = false;
return $address_fields;
}
// For billing email and phone fields
add_filter(‘woocommerce_billing_fields’, ‘optional_checkout_fields1’, 1000, 1);
function optional_checkout_fields1( $fields ) {
$fields[‘billing_email’][‘required’] = true;
$fields[‘billing_phone’][‘required’] = false;
return $fields;
}
And once more, if you need a only couple of fields to be optional then just paste the lines you need. For example, this snippet makes country and phone fields optional.
add_filter( ‘woocommerce_default_address_fields’ , ‘optional_default_address_fields’ );
function optional_default_address_fields( $address_fields ) {
$address_fields[‘country’][‘required’] = false;
return $address_fields;
}
// For billing email and phone fields
add_filter(‘woocommerce_billing_fields’, ‘optional_checkout_fields1’, 1000, 1);
function optional_checkout_fields1( $fields ) {
$fields[‘billing_phone’][‘required’] = false;
return $fields;
}