javascript - 在 Wordpress 服务器端使用 PHP 缩小 CSS 和 JS

标签 javascript php css wordpress

我希望将其添加到我的框架中以进一步优化它。我不想使用插件,我希望代码尽可能轻量。这是我到目前为止的代码。如果我只调用“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;
}

这是使用 minify 文件仅调用 style.css 时的输出。即当我删除“global.css”和“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)

关于javascript - 在 Wordpress 服务器端使用 PHP 缩小 CSS 和 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31918078/

相关文章:

Javascript for循环直到 - 多个条件

javascript - Div 不是从随机位置开始的

php - 在where子句中使用数组,其中检查字段包含逗号分隔的单词

javascript - 使用 JQuery 调整浏览器的宽度

javascript - 电子邮件模板的背景图像在 outlook 任何版本中均不起作用

javascript - 从数据库中获取 jQuery slider 值

javascript - 防止底层 div 在 iOS Safari 中滚动

PHP方法存在于类中而不创建对象

php - 从日期数组中查找上一个和下一个最近的日期

CSS 内联 block 问题 - 元素被下推