Subscribe via

Tutorial: Get WP Minify to Process Your JS and CSS Files

Thaya Kareeson

This tutorial only applies to WP Minify pre-2.0.

You should use this tutorial if you still notice some Javascript and CSS files not being processed by WP Minify after activating the plugin.

Problem

WP Minify only sees scripts and styles enqueued via the wp_enqueue_script() and wp_enqueue_style() function. So if your theme or plugin does not use these functions for printing Javascript or CSS references, then WP Minify will not include them into the Minifying process.

How to Enqueue Scripts

To enqueue a Javascript file use the following PHP contruct:

function my_load_js() {
  wp_enqueue_script('my_handle_name', 'http://example.com/path/to/file.js');
}
add_action('init','my_load_js');

To enqueue a Javascript file that is required to be loaded after another Javascript file, you can specify the dependent script’s handle as the third parameter. The following example will load second_file.js after first_file.js:

function my_load_js() {
  wp_enqueue_script('my_handle_name_first', 'http://example.com/path/to/first_file.js');
  wp_enqueue_script('my_handle_name_second', 'http://example.com/path/to/second_file.js', 'my_handle_name_first');
}
add_action('init','my_load_js');

More information on wp_enqueue_script() on WordPress Codex.

Make sure you add_action on the init hook and not the wp_print_scripts or wp_print_styles hook. It is too late for WP Minify to intercept these scripts and styles once it gets to the wp_print_scripts and wp_print_styles hooks.

How to Enqueue Styles

Use the same construct as the previous section except with the wp_enqueue_style() function instead.

Final Words

I have always been a believer of wp_enqueue_script() and wp_enqueue_style() functions. If your plugin is not using these functions yet, please upgrade it so that it can take advantage the WP Minify engine! If you do not own the plugin, please nudge your plugin author towards this article and request that they upgrade their plugin!

Save and Share
Del.icio.us
StumbleUpon
Digg
Reddit

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Currently experiencing problems with Intense Debate commenting system. Please make sure your browser's Javascript is enabled and try again later.