代码之家  ›  专栏  ›  技术社区  ›  WebDevBooster

如何正确保存包含HTML代码的WordPress选项?

  •  0
  • WebDevBooster  · 技术社区  · 7 年前

    以下是问题的截图: enter image description here

    WordPress选项页面的HTML如下所示: enter image description here

    所以,在WordPress管理区,我输入了一段HTML代码(在前端有一个可点击的链接作为输出)。

    这是我在后端的文本输入字段中输入的代码:

    <a title="Website Design London" href="../../website-design/">Website Design</a>
    

    而在前端的链接显示正常,我看到这个混乱(见屏幕截图)在后端。

    据我所知,相关的PHP代码如下:

    $this->text(
        'workdone',
        esc_html__( 'Work done', 'mytheme' )
    );
    

    那么,保存包含HTML代码的选项的正确方法是什么?

    我怎样才能修复截图上的混乱?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ivnhal    7 年前

    我也有同样的问题,这个代码对我很有用:

    // render service label
    public function render_service_label() {
        $value = get_option( 'wbk_service_label', '' ); 
        $value = htmlspecialchars( $value );
        $html = '<input type="text" id="wbk_service_label" name="wbk_service_label" value="'.$value.'" >';
            $html .= '<p class="description">' . __( 'Service label', 'wbk' ) . '</p>';
            echo $html;
    }
    // validate service label
    public function validate_service_label( $input ) {
        $allowed_tags = array(
            //formatting
            'strong' => array(),
            'em'     => array(),
            'b'      => array(),
            'i'      => array(),
            'br'      => array(),
            //links
            'a'     => array(
                'href' => array(),
                'class' => array()
            ),
            //links
            'p'     => array(
                'class' => array()
            )
        );
        $input =  wp_kses( $input, $allowed_tags );
        return  $input;
    }
    

    所以,在“仪表板选项”页中,我使用 htmlspecialchars 功能

    在frontend页面中,我这样使用:

    $label = get_option( 'wbk_service_label',  __( 'Select service', 'wbk' ) );