Enqueue wordpress styles and scripts without worrying about browser cache

/**
 * Enqueue a CSS stylesheet the same way the original function would, except automatically including the
 * most recent update timestamp to ensure the browser is told to load the latest file.
 *
 * Registers the style if source provided (does NOT overwrite) and enqueues.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::enqueue()
 *
 * @param string           $handle Name of the stylesheet. Should be unique.
 * @param string           $src    Full URL of the stylesheet, or path of the stylesheet relative to the THEME root directory.
 *                                 Default empty.
 * @param array            $deps   Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
 * 
 * @param string           $media  Optional. The media for which this stylesheet has been defined.
 *                                 Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
 *                                 '(orientation: portrait)' and '(max-width: 640px)'.
 */
function wp_enqueue_style_cache_killer($handle,$src,$deps = array(), $media = 'all'){
    
    $uri = get_template_directory_uri() . $src;
    $local = get_template_directory().$src;
    if (file_exists($local)) {
        $time = filemtime($local);
        wp_enqueue_style($handle,$uri,$deps,$time,$media);
    }
    
}


/**
 * Enqueue a script the same way the original function would, except automatically including the
 * most recent update timestamp to ensure the browser is told to load the latest file.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the <b>THEME</b> root directory.
 *                                    Default empty.
 * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.

 * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
 *                                    Default 'false'.
 */
function wp_enqueue_script_cache_killer($handle,$src,$deps = array(), $in_footer = false){
    
    $uri = get_template_directory_uri() . $src;
    $local = get_template_directory().$src;
    if (file_exists($local)) {
        $time = filemtime($local);
        wp_enqueue_script($handle,$uri,$deps,$time,$in_footer);
    }
    
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *