There are various plugins that optimize Wordpress/Woocommerce to make your page load faster. But adding more plugins itself reduces the speed and performance of your site. If you are comfortable editing code, then some of the smaller enhancements can be done better by adding just a few lines of code to your themes's functions.php
Disable emoji
Starting from Wordpress 4.2 emoji support was added into Wordpress core and this intoduces unnecessary JavaScipt and code to your pages. In most cases Wordpress websites dont require these. The following code will remove all emoji related scripts and CSS.
//Disable emoji add_action( 'init', 'disable_emoji' ); function disable_emoji() { // 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' ); // new filter to remove TinyMCE emojis add_filter( 'tiny_mce_plugins', 'disable_tiny' ); //new filter to remove dns prefetch add_filter( 'emoji_svg_url', '__return_false' ); } function disable_tiny( $plugins ) { if ( is_array( $plugins ) ) { return array_diff( $plugins, array( 'wpemoji' ) ); } else { return array(); } }
Remove query strings for static resources
Resources with query strings are not cached by some servers. Your page may also rank lower in speed tests like Pingdom and GTMatrix if you load CSS and JS with query strings. To remove these just add the code below to functions.php
:
//Remove query strings from static resources
function ewp_remove_script_version( $src ){
return remove_query_arg( 'ver', $src );
}
add_filter( 'script_loader_src', 'ewp_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', 'ewp_remove_script_version', 15, 1 );
Remove unnecessary Woocommerce styles and scripts
Once you add Woocommerce to your site, it queues up all the Wordpress styles and scripts on all pages, but you may not need all those scripts on all pages. The following code snippet dequeues the scripts and styles for all pages except for certain pages in the if clause.
//Dequeue unnecessary scripts on non-woo pages add_action( 'wp_enqueue_scripts', 'exclude_woocommerce_styles', 99 ); function exclude_woocommerce_styles() { //remove generator meta tag remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) ); //check woo exists if ( function_exists( 'is_woocommerce' ) ) { //dequeue scripts and styles if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) { wp_dequeue_style( 'woocommerce_frontend_styles' ); wp_dequeue_style( 'woocommerce_fancybox_styles' ); wp_dequeue_style( 'woocommerce_chosen_styles' ); wp_dequeue_style( 'woocommerce_prettyPhoto_css' ); wp_dequeue_script( 'wc_price_slider' ); wp_dequeue_script( 'wc-single-product' ); wp_dequeue_script( 'wc-add-to-cart' ); wp_dequeue_script( 'wc-cart-fragments' ); wp_dequeue_script( 'wc-checkout' ); wp_dequeue_script( 'wc-add-to-cart-variation' ); wp_dequeue_script( 'wc-single-product' ); wp_dequeue_script( 'wc-cart' ); wp_dequeue_script( 'wc-chosen' ); wp_dequeue_script( 'woocommerce' ); wp_dequeue_script( 'prettyPhoto' ); wp_dequeue_script( 'prettyPhoto-init' ); wp_dequeue_script( 'jquery-blockui' ); wp_dequeue_script( 'jquery-placeholder' ); wp_dequeue_script( 'fancybox' ); wp_dequeue_script( 'jqueryui' ); } } }
The scripts are dequeued all pages except cart, checkout and other Woocommerce pages. You can edit the if
clause and give other conditions. You may also add/remove scripts in the list if you have better understanding of what scripts are needed or not needed for a page.
Remove related products and upsells from product page
Woocommerce displays related products and upsells on a product page and sometimes you want to remove them so that the product page loads faster. Here is how to do this by adding a bit of code to the theme's functions.php
.
// Remove related products from after single product hook remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); // Remove up sells from after single product hook remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
With these minor tweaks in place you should be able to see a significant improvement in the load time of your pages.