简体   繁体   中英

Wordpress enqueue style dependency loads out of order

So, i've built myself an enqueue'd stylesheet concatenator, while it works, what I've found is that the "dependencies" seem to be out of order... in this instance, I deregister and dequeue my "custom" stylesheet, register and enqueue my concatenated stylesheet, and then "re" register and "re" enqueue my custom style sheet using the concatenated handle as the dependency for it... and it always loads before the concatenated stylesheet.

Any ideas why this may be happenning? In my child-theme, I am enqueing "custom" at the default priority, in the parent theme, is where I am performing the concatenation, and my action has the highest possibly priority.

The Code:

protected function concatenate_css( $_cache_time ) {
    add_action( 'wp_enqueue_scripts', function( ) use ( $_cache_time ) {
        // need our global
        global $wp_styles;

        // our css string holder
        $_css_string = '';

        // path to the theme
        $_theme_path = get_stylesheet_directory( );

        // uri to the theme
        $_theme_uri = get_stylesheet_directory_uri( );

        // new file path
        $_new_css = $_theme_path . '/style.concat.css';

        // force the order based on the dependencies
        $wp_styles -> all_deps( $wp_styles -> queue );

        // setup our exclusions
        $_exclude = array( 'custom', 'concatcss', );

        // loop through everything in our global
        foreach( $wp_styles -> queue as $_hndl ) {

            // get the source from the hanlde
            $_path = $wp_styles -> registered[$_hndl]->src;

            // if we have a "custom" handle, we do not want to process it, so ... skip it
            // we also do want to process any source that is not set
            if( ! in_array( $_hndl, $_exclude ) && $_path ) {

                // we also only want to do this for local stylehseets
                if ( strpos( $_path, site_url( ) ) !== false ) {

                    $_path = ABSPATH . str_replace( site_url( ), '', $_path );

                    // now that we have everything we need, let's hold the contents of the file in a string variable, while concatenating
                    $_css_string .= file_get_contents( $_path ) . PHP_EOL;

                    // now remove the css from queue
                    wp_dequeue_style( $_hndl );
                    // and deregister it
                    wp_deregister_style( $_hndl );

                }
            }
        }

        // dequeue and deregsiter any "custom" style
        wp_dequeue_style( 'custom' );
        wp_deregister_style( 'custom' );

        // now write out the new stylesheet to the theme, and enqueue it
        // check the timestamp on it, versus the number of seconds we are specifying to see if we need to write a new file
        if( file_exists( $_new_css ) ) {
            $_ftime = filemtime( $_new_css ) + ( $_cache_time );
            $_ctime = time( );
            if( ( $_ftime <= $_ctime ) ) {
                file_put_contents( $_new_css, $_css_string );
            }
        } else {
            file_put_contents( $_new_css, $_css_string );
        }

        wp_register_style( 'concatcss', $_theme_uri . '/style.concat.css', array( ), null );
        wp_enqueue_style( 'concatcss' );

        // requeue and reregister our custom stylesheet, using this concatenated stylesheet as it's dependency
        wp_register_style( 'custom', $_theme_uri . '/css/custom.css?_=' . time( ), array( 'concatcss' ), null );
        wp_enqueue_style( 'custom' );

    }, PHP_INT_MAX );

}

Screenshot of the page source

在此处输入图片说明

Just register your concatenated script, but don't enqueue it.

    wp_register_style( 'concatcss', $_theme_uri . '/style.concat.css', array( ), null );

    // requeue and reregister our custom stylesheet, using this concatenated stylesheet as it's dependency
    wp_enqueue_style( 'custom', $_theme_uri . '/css/custom.css?_=' . time( ), array( 'concatcss' ), null );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM