个性化阅读
专注于IT技术分析

WordPress排队样式依赖项加载不正常

因此, 我为自己构建了一个入队的样式表连接器, 当它起作用时, 我发现”依赖项”似乎混乱了……在这种情况下, 我注销并退出了我的”自定义”样式表, 注册并排队我的串联样式表, 然后使用串联句柄作为其依赖项”重新”注册并”重新”排队我的自定义样式表…它始终在串联样式表之前加载。

任何想法为什么会发生这种情况?在我的孩子主题中, 我以默认优先级将”自定义”放入父级主题中, 在该父级中, 我正在执行连接, 并且我的操作具有最高优先级。

编码:

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 );

}

页面源的屏幕截图

在此处输入图片说明

#1


只需注册你的串联脚本, 但不要使其入队。

    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 );
赞(0)
未经允许不得转载:srcmini » WordPress排队样式依赖项加载不正常

评论 抢沙发

评论前必须登录!