无法正确设置WordPress Add_Filter函数

时间:2017-02-07 作者:Jenia Sliusarenko

我正在尝试使用将图标添加到按钮add_filter() 功能:

function sh_woocompare_button_icon($text)
{
 return  \'<i class="fa fa-heart-o" aria-hidden="true"></i> <span class="tm_woocompare_product_actions_tip"><span class="text">\' . $text  . \'</span></span>\';
}
add_filter( \'tm_woocompare_button\', \'sh_woocompare_button_icon\');
我想对函数应用更改:

function tm_woocompare_add_button( $args ) {
    $id      = get_the_ID();
    $id      = tm_wc_compare_wishlist()->get_original_product_id( $id );
    $classes = array( \'button\', \'tm-woocompare-button\', \'btn\', \'btn-default\' );
    $nonce   = wp_create_nonce( \'tm_woocompare\' . $id );

    if ( in_array( $id, tm_woocompare_get_list() ) ) {
        $text      = get_option( \'tm_woocompare_remove_text\', __( \'Remove from Compare\', \'tm-wc-compare-wishlist\' ) );
        $classes[] = \' in_compare\';
    } else {
        $text = get_option( \'tm_woocompare_compare_text\', __( \'Add to Compare\', \'tm-wc-compare-wishlist\' ) );
    }
    $text      = \'<span class="tm_woocompare_product_actions_tip"><span class="text">\' . esc_html( $text ) . \'</span></span>\';
    $preloader = apply_filters( \'tm_wc_compare_wishlist_button_preloader\', \'\' );

    if( $single = ( is_array( $args ) && isset( $args[\'single\'] ) && $args[\'single\'] ) ) {
        $classes[] = \'tm-woocompare-button-single\';
    }
    $html = sprintf( \'<button type="button" class="%s" data-id="%s" data-nonce="%s">%s</button>\', implode( \' \', $classes ), $id, $nonce, $text . $preloader );

    echo apply_filters( \'tm_woocompare_button\', $html, $classes, $id, $nonce, $text, $preloader );

    if( in_array( $id, tm_woocompare_get_list() ) && $single ) {
        echo tm_woocompare_page_button();
    }
}
因此$text 变量将整个按钮代码输出为简单文本。

enter image description here

1 个回复
SO网友:Jenia Sliusarenko

这是解决方案!

function woocompare_button_icon( $html, $classes, $id, $nonce, $text, $preloader )
{
    return sprintf( \'<button type="button" class="%s" data-id="%s" data-nonce="%s"><i class="fa fa-heart-o" aria-hidden="true"></i> %s</button>\', implode( \' \', $classes ), $id, $nonce, $text . $preloader );
}

add_filter( \'tm_woocompare_button\', \'woocompare_button_icon\', 10, 6);