纯代码实现WordPress前端Html压缩代码实现优化

 将下面代码添加到主题中的function.php中

/*
*纯代码实现html压缩
*https://www.dayuvlog.xyz/536.html
*/
function wp_compress_html(){
    function wp_compress_html_main ($buffer){
        $initial=strlen($buffer);
        $buffer=explode("<!--wp-compress-html-->", $buffer);
        $count=count ($buffer);
        for ($i = 0; $i <= $count; $i++){
            if (stristr($buffer[$i], '<!--wp-compress-html no compression-->')) {
                $buffer[$i]=(str_replace("<!--wp-compress-html no compression-->", " ", $buffer[$i]));
            } else {
                $buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
                $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
                $buffer[$i]=(str_replace("\n", "", $buffer[$i]));
                $buffer[$i]=(str_replace("\r", "", $buffer[$i]));
                while (stristr($buffer[$i], '  ')) {
                    $buffer[$i]=(str_replace("  ", " ", $buffer[$i]));
                }
            }
            $buffer_out.=$buffer[$i];
        }
        $final=strlen($buffer_out);   
        $savings=($initial-$final)/$initial*100;   
        $savings=round($savings, 2);   
        $buffer_out.="\n<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->";   
    return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');

相关wordpress教程..........

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是代码实现WordPress添加网站公告功能的 PHP 代码: ```php // 添加公告 function add_site_notice($notice_text) { $option_name = 'site_notice'; $option_value = $notice_text; update_option($option_name, $option_value); } // 显示公告 function show_site_notice() { $option_name = 'site_notice'; $notice_text = get_option($option_name); if ($notice_text) { echo '<div class="site-notice">' . $notice_text . '</div>'; } } // 在 WordPress 后台添加公告编辑框 function add_site_notice_editor() { $option_name = 'site_notice'; $notice_text = get_option($option_name); echo '<label for="' . $option_name . '">网站公告:</label>'; echo '<textarea id="' . $option_name . '" name="' . $option_name . '">' . $notice_text . '</textarea>'; } // 保存公告编辑框中的内容 function save_site_notice_editor() { $option_name = 'site_notice'; if (isset($_POST[$option_name])) { $notice_text = $_POST[$option_name]; update_option($option_name, $notice_text); } } // 在 WordPress 后台添加公告编辑框 add_action('admin_init', function() { add_settings_field( 'site_notice', '网站公告', 'add_site_notice_editor', 'general' ); register_setting('general', 'site_notice', 'save_site_notice_editor'); }); // 在 WordPress 前台显示公告 add_action('wp_footer', 'show_site_notice'); ``` 将上述代码添加到 WordPress 主题的 `functions.php` 文件中即可。其中,`add_site_notice()` 函数用于添加公告,`show_site_notice()` 函数用于显示公告,`add_site_notice_editor()` 函数用于在 WordPress 后台添加公告编辑框,`save_site_notice_editor()` 函数用于保存公告编辑框中的内容。最后,使用 WordPress 提供的 `add_settings_field()` 和 `register_setting()` 函数将公告编辑框添加到 WordPress 后台的“常规设置”中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值