1

How can I add a link to a sidebar description?

$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>',
    )
);

This will output the A-tag in pure text instead of HTML.

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 );
});
3

The sidebar description runs through esc_html(), so you cannot pass HTML directly. But you can use the filter esc_html to insert your desired content.

Here is a simple example:

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 );
    });
});

Result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.