Skip to main content

Remove unused Wordpress stylesheets and scripts - Just code it!

 


Just have found a good way to remove unnecessary css and js links on your website header. If you aren't familiar with a good page load auditor, you might visit Gtmetrix.com, Pingdom.com, and Google Page Speed to check your site's performance.


Unused CSS and JS scripts makes your site's page load so heavy and sometimes it turns over 10 to 30 seconds to display your frontend on the browser. Thus, this way could also give you more idea on how do you manage your site's performance. 


I have tried several times the use of wp_dequeue_style() functions both in my child and parent theme but didn't worked. However, I never liked contact form 7 to run out in my front-end since before I get into Wordpress platform so I often removed it by adding code define( 'WPCF7_LOAD_CSS', false ) into wp-config.php just below define( 'WP_DEBUG', false ). So let's get started.

You must first check the running css on your front-end on browser element inspector. On the header of your webpage, look for the id as you can see on the following screenshot.


After copying the stylesheet or script id, get it into function.php and do the same way with the code below:

Note: Always exclude "-css" or "-js" each id.

Removing CSS Stylesheet

<?php
add_action( 'wp_print_styles', 'deregister_cssstyles', 9999 ); function deregister_cssstyles() { global $wp_styles; $wp_styles->remove('dashicons'); // "-css" should not be included $wp_styles->remove('google-fonts-1'); // You can add more you want }

 

Removing JS Scripts

<?php
add_action( 'wp_print_scripts', 'deregister_jscripts', 9999 ); function deregister_jscripts() { global $wp_scripts; $wp_styles->remove('contact-form-js-extra'); // "-js" should not be included $wp_styles->remove('reviewmgr'); // You can add more you want }

 Codes above could be a primary altenative to standard way of dequeuing styles and scripts. Like this:

<?php
//Remove Gutenberg Block Library CSS from loading on the frontend
function smartwp_remove_wp_block_library_css(){
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
wp_dequeue_style( 'wc-block-style' ); // Remove WooCommerce block CSS
}
add_action( 'wp_enqueue_scripts', 'smartwp_remove_wp_block_library_css', 100 );

 However, the wp builtin function often don't works in parent theme.


Comments

Popular posts from this blog

Working Dark Web links in 2021

.Onion v2 will be deprecated in October 2021. v3 .onion sites will be formally launched. Yet, if you are wondering what's all about .onion, download Tor Browser and install to your computer. Tor will connects you to the deep web often called "hidden sites" and some calls it "dark web". Note: Never open the links if you do not have information about .onion sites.  FAQ What are .onion links?   onion links are only accessible via the Tor Network, commonly known as the "dark web"   How do I access .onion links?   you need the Tor Browser, which is a modified version of Firefox that routes any traffic through the Tor Network   Is accessing the Tor Network/Dark Web illegal?   a quick search in your prefered search engine will give you the answer  How do I stay safe while browsing the dark web?   only use the Tor Browser on a Unix based OS (macOS/OSX and Linux (any distro/flavor will do)) and have a VPN that does not ...

LET Grading System: Elementary and Secondary Levels Explained with Examples

The Licensure Examination for Teachers (LET) is an important examination that aspiring teachers must take in order to obtain their teaching license in the Philippines. In this tutorial, we will explore the grading system for the LET, specifically for the Elementary Level and Secondary Level where "x" is the candidate's average each category such as general education and professional education. Elementary Level: General Education: 60% * x  Professional Education: 40% * x Example: If a candidate scores 80 in General Education and 90 in Professional Education, the calculation would be as follows: General Education score = 80 * 0.6 = 48 Professional Education score = 90 * 0.4 = 36 Final Score = 48 + 36 = 84 Secondary Level: General Education: 20% * x  Professional Education: 40% * x  Major: 40% * x  Example: If a candidate scores 75 in General Education, 85 in Professional Education, and 80 in Major, the calculation would be as follows: General Education score = 75 * 0.2...

PHP 8 deprecates in Wordpress: class-wp-list-table.php:717

There has been released latest versions PHP 8.0 and 8.1 but Wordpress and some of its plugins are not ready to convey the patterns provided in PHP 8. I just found today that after upgrading a website from 7.4, a lot of error notifications appeared everywhere in the website when enabling of debug mode. However, we will focused on the given title in this blog as important matter in order to check the status of the orders if approved, pending or cancelled. This content lets you spend very short time in finding out errors in Wordpress 6.0 with PHP 8.0 most specifically the one I had figured out from this notice "Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /www/.../wp-admin/includes/class-wp-list-table.php:717". However, in our case, the website has an installed plugins "Advanced Custom Field Pro" and "Woocommerce 7.3" which combined to manipulate order statuses. The website has an e-commerce platform that vi...