在Wordpress服务器端使用PHP最小化CSS和JS


minify CSS and JS with PHP in Wordpress server side

我希望将此添加到我的框架中以进一步优化它。我不想使用插件,我希望代码尽可能轻的重量。这是我目前掌握的代码。如果我只调用"style.css",这是有效的,但如果我调用其他文件,它不会缩小文件。我得到相同的js文件,我有。它删除了空白,但没有完全缩小文件。希望有人能帮忙。由于

    <?php
$dev_master_root = dirname(dirname(dirname(dirname(dirname(__FILE__)))));
if ( file_exists( $dev_master_root.'/wp-load.php' ) ) {
    require_once( $dev_master_root.'/wp-load.php' );
} elseif ( file_exists( $dev_master_root.'/wp-config.php' ) ) {
    require_once( $dev_master_root.'/wp-config.php' );
}  
/* Add your CSS files to this array (THESE ARE ONLY EXAMPLES) */
$cssFiles = array(
  "../style.css",
  "global.css",
  "slicknav.css"
);
$buffer = "";
foreach ($cssFiles as $cssFile) {
  $buffer .= file_get_contents($cssFile);
}
// Remove comments
$buffer = preg_replace('!/'*[^*]*'*+([^/][^*]*'*+)*/!', '', $buffer);
// Remove space after colons
$buffer = str_replace(': ', ':', $buffer);
// Remove whitespace
$buffer = str_replace(array("'r'n", "'r", "'n", "'t", '  ', '    ', '    '), '', $buffer);
// Enable GZip encoding.
ob_start("ob_gzhandler");
// Enable caching
header('Cache-Control: public');
// Expire in one day
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
// Set the correct MIME type, because Apache won't set it for us
header("Content-type: text/css");
// Write everything out
echo($buffer);
?>

部分的输出与上面的最小化文件完全相同。

    body {
    color: #2d2e32;
}
h1 {
    font-size: 24px;
}
h2 {
    font-size: 21px;
}
h3 {
    font-size: 18px;
}
h4 {
    font-size: 16px;
}
h5 {
    font-size: 12px;
}
h6 {
    font-size: 11px;
}
h1, h2, h3, h4, h5, h6 {
    line-height: 1.618;
    margin-bottom: 20px;
}
p {
    font-family: 'Domine', serif;
    font-size: 12px;
}
#dev-master-main-container {}
#dev-master-header-container {
    background: #fff;
}
#dev-master-content-container {}
#dev-master-footer-container {
    background: #000026;
}
#dev-master-footer-content {}
@media screen and (max-width :1180px) {}
@media screen and (max-width :960px) {}
@media screen and (max-width :768px) {}
@media screen and (max-width :524px) {}
html {
    font-family: sans-serif;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    height: 100%;
}
body {
    margin: 0;
    height: 100%;
}
article, aside, details, figcaption, figure, footer, header, main, menu,
nav, section, summary {
    display: block;
}
audio, canvas, progress, video {
    display: inline-block;
    vertical-align: baseline;
}
audio:not([controls]) {
    display: none;
    height: 0;
}
[hidden], template {
    display: none;
}
a {
    background-color: transparent;
}
a:active, a:hover {
    outline: 0;
}
abbr[title] {
    border-bottom: 1px dotted;
}
b, strong {
    font-weight: 700;
}
dfn {
    font-style: italic;
}
h1 {
    font-size: 2em;
    margin: .67em 0;
}
mark {
    background: #ff0;
    color: #000;
}
small {
    font-size: 80%;
}
sub, sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
}
sup {
    top: -.5em;
}

这是使用最小化文件只调用style.css时的输出。例如,当我删除"global.css"answers"slicknav.css"

h1,h2,h3,h4,h5,h6 {}p {}#dev-master-main-container {}#dev-master-header-container {background:#fff;}#dev-master-content-container {}#dev-master-footer-container {background:#000026;}#dev-master-footer-content {}@media screen and (max-width :1180px) {}@media screen and (max-width :960px) {}@media screen and (max-width :768px) {}@media screen and (max-width :524px) {}

在上面的代码中,这只是style.css,但是你可以看到所有的注释等都被剥离了。因为所有的基础样式都是从global。css

调用的

我刚刚注意到这段代码中的空格,我也想删除。我认为上面的字符串会这样做…?

如果你需要更多的信息,请告诉我。再次感谢

您是否在数组中正确引用文件位置?

同样,在你现有的代码中,它不会执行完整的缩小,因为这涉及到将变量名称切换为较短的名称,例如(someVariableName将成为a)