如何将链接添加到侧边栏描述?

时间:2015-05-28 作者:Mark

如何向侧栏描述添加链接?

$sidebar_description = sprintf(__( \'Sidebar for the <a href="%s">%s</a> post\', \'themetext\' ), get_permalink($post->ID), $post->post_title);

register_sidebar(
    array(
        \'name\'              => $post->post_title . \' (\' .  __( \'Post\', \'themetext\' ) . \')\',
        \'id\'                => \'sidebar-\'.$post->ID,
        \'description\'       => $sidebar_description,
        \'before_widget\'     => \'<div id="%1$s" class="widget %2$s">\',
        \'after_widget\'      => \'</div>\',
        \'before_title\'      => \'<h3>\',
        \'after_title\'       => \'</h3>\',
    )
);
这将以纯文本而不是HTML输出A标记。

ANSWER

add_action( \'widgets_admin_page\', function() use ( $sidebar_description ) {
    add_filter( \'esc_html\', function( $safe_text, $text ) use ( $sidebar_description ) {
        if ( $text !== $sidebar_description )
            return $safe_text;

        remove_filter( current_filter(), __FUNCTION__ );

        return $sidebar_description;
    }, 10, 2 );
});

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

侧栏描述贯穿整个esc_html(), 因此,您无法直接传递HTML。但你可以使用过滤器esc_html 插入所需内容。

下面是一个简单的示例:

add_action( \'widgets_init\', function() {

    $desc = "Read the <a href=\'http://wordpress.stackexchange.com/q/189749/73\'>explanation</a>!";
    $placeholder = \'PLACEHOLDER\';
    register_sidebar([
        \'id\'          => \'wpse-189749\',
        \'name\'        => \'Link description\',
        \'description\' => $placeholder
    ]);

    add_action( \'widgets_admin_page\', function() use ( $desc, $placeholder ) {
        add_filter( \'esc_html\', function( $safe_text, $text ) use ( $desc, $placeholder ) {

            if ( $text !== $placeholder )
                return $safe_text;

            remove_filter( current_filter(), __FUNCTION__ );

            return $desc;
        }, 10, 2 );
    });
});
结果:

enter image description here

结束