rog*_*ila 5 html php wordpress action minify
我正在使用此代码来缩小wordpress中的HTML输出.它在主页面和帖子页面上都很完美,但在管理部分它会导致很多问题.
function minify_html(){
ob_start('html_compress');
}
function html_compress($buffer){
$search = array(
'/\n/', // replace end of line by a space
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences,
'~<!--//(.*?)-->~s' //html comments
);
$replace = array(
' ',
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
add_action('wp_loaded','minify_html');
Run Code Online (Sandbox Code Playgroud)
使用'the_post'代替'wp_loaded'只会缩小帖子,但我希望能够将主页面和帖子页面缩小到100%,但管理部分没有任何内容.如何组合操作以进行管理?
谢谢!
小智 3
不错的代码,排除管理员:
if (!(is_admin() )) {
function minify_html(){
ob_start('html_compress');
}
function html_compress($buffer){
$search = array(
'/\n/', // replace end of line by a space
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences,
'~<!--//(.*?)-->~s' //html comments
);
$replace = array(
' ',
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
add_action('wp_loaded','minify_html'); }
Run Code Online (Sandbox Code Playgroud)
WP 管理效果很好!