dongpaozhi5734 2019-06-23 17:37
浏览 76
已采纳

根据WP编码标准清理和清理php代码

I have few lines of code which i need to clean up as per WP coding standard using the functions like esc_html, wp_kses() etc (https://developer.wordpress.org/reference/functions/wp_kses/). That code generates a printing button on the post detail page. The problem is i can't figure out the correct way to use wp_kses function so as to show the button on the page. Could anyone help me here? I am a beginner for php. Thanks in advance!

Code on the file

<!-- printer friednly link  -->
    <?php if(function_exists('pf_show_link')){echo pf_show_link();} ?>

Code i am trying to add which is not outputting any button

<!-- printer friednly link  -->
    <?php
    if ( function_exists( 'pf_show_link' ) ) {
        $allowed_tags = array(
            'strong' => array(),
            'a'      => array(
                'href'  => array(),
                'title' => array(),
            ),
        );
        echo wp_kses( $allowed_tags );
    }
    ?>

the button which renders from the code

  • 写回答

1条回答 默认 最新

  • doze79040 2019-06-24 10:55
    关注

    I am not sure you should use wp_kses here at all. If you are the one who generates that html output with function, you don't need to sanitize. Anyway, if you really want to, you must list all html tags you use (so also, div, img...) and all attributes inside those tags (style, src, onclick...). Try like this:

    $allowed_tags = array(
        'div'    => array(),
        'img'    => array(
            'style' => array(),
            'src' => array(),
            'alt' => array()
        ),
        'strong' => array(),
        'a'      => array(
            'href'  => array(),
            'title' => array(),
            'rel' => array(),
            'onclick' => array(),
            'class' => array(),
            'data-wpel-link' => array()
        ),
    );
    
    echo wp_kses(pf_show_link(), $allowed_tags);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵