[英]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);
?>
與minify文件完全相同的輸出部分。
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
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.