Skip to content

Minit Action and Filter Hooks

Viktor Szépe edited this page Nov 30, 2016 · 5 revisions

Available Action Hooks

minit-cache-purged

Called when the global cache gets purged.

minit-cache-purge-delete

This is not called but hooked by Minit, you may clear the cache with do_action( 'minit-cache-purge-delete' );

Available Filter Hooks

minit-js-in-footer

@todo create a fallback

minit-script-tag-async

Disable async script loading. (boolean)

minit-exclude-{css,js}

Exclude specific style and script handles. (array)

// Exclude handles that are known to cause problems
add_filter( 'minit-exclude-js', 'minit_exclude_defaults' );

function minit_exclude_defaults( $handles ) {

	$exclude = array(
		'this-is-a-handle-of-a-script-you-want-to-exclude',
	);

	return array_merge( $exclude, $handles );

}

minit-url-{css,js}

Modify minited file URL-s.

minit-content-{css,js}

Change the content of minited files.

add_filter( 'minit-content-js', 'minit_jsqueeze' );
add_filter( 'minit-content-css', 'minit_cssmin' );

function minit_jsqueeze( $javascript ) {

    // https://github.com/tchwork/jsqueeze
    require_once 'lib/JSqueeze.php';

    $jz = new Patchwork\JSqueeze();
    $content = $jz->squeeze( $javascript, true, true, false );

    return $content;

}

function minit_cssmin( $stylesheet ) {

    // https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port
    require_once 'lib/cssmin.php';
    $compressor = new CSSmin();
    $content = $compressor->run($content);

    return $content;

}

minit-cache-expiration

Default cache expiration time of all minited files' data.

minit-cache-expiration-{css,js}

Cache expiration time of minited style's and script's data.

minit-asset-local-path

Support custom directory structure.

add_filter( 'minit-asset-local-path', 'minit_custom_content_dir', 10, 2 );
function minit_custom_content_dir( $local_path, $item_url ) {

	// Bail out if get_local_path_from_url() succeeded
	if ( false !== $local_path ) {
		return $local_path;
	}

	// Replace custom MU plugin URL
	$full_path = str_replace( WPMU_PLUGIN_URL, WPMU_PLUGIN_DIR, $item_url );
	// Replace custom plugin URL
	$full_path = str_replace( plugins_url(), WP_PLUGIN_DIR, $full_path );
	// Replace custom theme URL
	$full_path = str_replace( get_theme_root_uri(), get_theme_root(), $full_path );
	// Replace remaining custom wp-content URL
	$full_path = str_replace( content_url(), WP_CONTENT_DIR, $full_path );

	if ( file_exists( $full_path ) ) {
		return $full_path;
	}

	return false;

}