WordPress主题框架Options Framework过滤常用标签

WordPress主题框架Options Framework过滤常用标签

这里就照搬知更鸟博客的,主要是他博客的这篇文章已经看不到代码了,找了半天才找到。以下是原文:相信很多主题作者,在使用Options Framework主题选项框架时都遇到一个棘手的问题,就是该框架出于安全会过滤掉常用标签,最关键是过滤掉加载 javascript的常用...

这里就照搬知更鸟博客的,主要是他博客的这篇文章已经看不到代码了,找了半天才找到。

以下是原文:

相信很多主题作者,在使用Options Framework主题选项框架时都遇到一个棘手的问题,就是该框架出于安全会过滤掉常用标签,最关键是过滤掉加载 javascript的常用标签,造成无法添加广告及站点统计代码,虽然通过使用编辑器模式替代textarea文本域,可解决上述问题,但主题控制面板都是编辑器窗口看上去有些怪异。

其实官方已给出解决办法:

Options Framework: Sanitization Filters
/*
* This is an example of how to override a default filter
* for ‘textarea’ sanitization and $allowedposttags + embed and script.
 */
add_action('admin_init','optionscheck_change_santiziation', 100);
function optionscheck_change_santiziation() {
    remove_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
    add_filter( 'of_sanitize_textarea', 'custom_sanitize_textarea' );
}
function custom_sanitize_textarea($input) {
    global $allowedposttags;
    $custom_allowedtags["embed"] = array(
      "src" => array(),
      "type" => array(),
      "allowfullscreen" => array(),
      "allowscriptaccess" => array(),
      "height" => array(),
      "width" => array()
      );
      $custom_allowedtags["script"] = array();
      $custom_allowedtags = array_merge($custom_allowedtags, $allowedposttags);
      $output = wp_kses( $input, $custom_allowedtags);
    return $output;
}

不过这个实例只是不过滤<script>标签,像这种:

<script type="text/javascript" src="zmingcx.js"></script>

还是会过滤掉type、src等标签,可能造成JS文件不能正常加载。

下面是经过我修改的完整不过滤  javascript 常用标签代码:

/*
 * This is an example of how to override a default filter
 * for ‘textarea’ sanitization and $allowedposttags + embed and script.
 */
add_action('admin_init','optionscheck_change_santiziation', 100);
function optionscheck_change_santiziation() {
    remove_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
    add_filter( 'of_sanitize_textarea', 'custom_sanitize_textarea' );
}
function custom_sanitize_textarea($input) {
    global $allowedposttags;
    $custom_allowedtags["embed"] = array(
        "src" => array(),
        "type" => array(),
        "allowfullscreen" => array(),
        "allowscriptaccess" => array(),
        "height" => array(),
        "width" => array()
      );
    $custom_allowedtags["script"] = array( "type" => array(),"src" => array() );
    $custom_allowedtags = array_merge($custom_allowedtags, $allowedposttags);
    $output = wp_kses( $input, $custom_allowedtags);
    return $output;
}

该代码在Options Framework 1.91版中测试通过,其它较早版本未测试。

个人感觉默认过滤常用标签根本没这个必要,可能就是在故意难为我这样的二把刀主题作者。

因为爱游博客使用的是自建的umami统计(Docker搭建自己的网站统计工具umami),所以还是得改下才能用代码如下:

//允许脚本和嵌入标签,设置中可以加入js统计代码
add_action('admin_init','optionscheck_change_santiziation', 100);
function optionscheck_change_santiziation() {
    remove_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
    add_filter( 'of_sanitize_textarea', 'custom_sanitize_textarea' );
}
function custom_sanitize_textarea($input) {
    global $allowedposttags;
    $custom_allowedtags["embed"] = array(
        "src" => array(),
        "type" => array(),
        "allowfullscreen" => array(),
        "allowscriptaccess" => array(),
        "height" => array(),
        "width" => array()
      );
    $custom_allowedtags["script"] = array( "type" => array(),"data-website-id" => array(),"src" => array() );
    $custom_allowedtags = array_merge($custom_allowedtags, $allowedposttags);
    $output = wp_kses( $input, $custom_allowedtags);
    return $output;
}

以上代码放置在主题的functions.php文件即可生效。

版权申明:
版权声明

①:本站文章均为原创,除非另有说明,否则本站内容依据CC BY-NC-SA 4.0许可证进行授权,转载请附上出处链接,谢谢。
②:本站提供的所有资源均为网上搜集,不保证能100%完整,如有涉及或侵害到您的版权请立即通知我们。
③:本站所有下载文件,仅用作学习研究使用,请下载后24小时内删除,支持正版,勿用作商业用途。
④:本站保证所提供资源的完整性,但不含授权许可、帮助文档、XML文件、PSD、后续升级等。
⑤:使用该资源需要用户有一定代码基础知识!由本站提供的资源对您的网站或计算机造成严重后果的本站概不负责。
⑥:本站资源售价只是赞助,收取费用仅维持本站的日常运营所需。
⑦:如果喜欢本站资源,欢迎捐助本站开通会员享受优惠折扣,谢谢支持!
⑧:如果网盘地址失效,请在相应资源页面下留言,我们会尽快修复下载地址。

0

评论0

请先

会员低至49元,开通享海量VIP资源免费下载 自助开通
显示验证码
没有账号?注册  忘记密码?