php - how can i add class or span tag at sprintf?

i hope to add span tag or class at sprintfI want to enlarge the text size of the numbers in front of me. So I want to ad

i hope to add span tag or class at sprintf

I want to enlarge the text size of the numbers in front of me. So I want to add span tag or class only to text in front of me.

my code is

            /**
             * Filters the product average rating.
             *
             * @since 1.10.0
             *
             * @param float $average_rating average rating
             * @param \WC_Product $product the product object
             */
            $average_rating = max( 0, (float) apply_filters( 'wc_product_reviews_pro_product_rating_average', $product->get_average_rating(), $product ) );

            $reviews_count  = max( 0, wc_product_reviews_pro_get_contributions_count( $product, 'review' ) );

            /* translators: Placeholders: %s - average rating stars count (float casted as string to avoid trailing zeroes), %d - 5 stars total (integer) -- (e.g "4.2 / 5 stars") */
            $reviews_label  = sprintf( __( '%s / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 );

            ?>
            <h3><?php echo esc_html( $reviews_label ); ?></h3>

            <p><?php echo get_star_rating(); ?></p>

I tried many ways. I've taken a lot of reference to the above url

But all the results are printed like <span>4</span>/5. I don't want it to be in print.

Is there a good solution? thanks.

i hope to add span tag or class at sprintf

I want to enlarge the text size of the numbers in front of me. So I want to add span tag or class only to text in front of me.

my code is

            /**
             * Filters the product average rating.
             *
             * @since 1.10.0
             *
             * @param float $average_rating average rating
             * @param \WC_Product $product the product object
             */
            $average_rating = max( 0, (float) apply_filters( 'wc_product_reviews_pro_product_rating_average', $product->get_average_rating(), $product ) );

            $reviews_count  = max( 0, wc_product_reviews_pro_get_contributions_count( $product, 'review' ) );

            /* translators: Placeholders: %s - average rating stars count (float casted as string to avoid trailing zeroes), %d - 5 stars total (integer) -- (e.g "4.2 / 5 stars") */
            $reviews_label  = sprintf( __( '%s / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 );

            ?>
            <h3><?php echo esc_html( $reviews_label ); ?></h3>

            <p><?php echo get_star_rating(); ?></p>

I tried many ways. https://stackoverflow/questions/45312511/adding-class-to-element-in-php-code I've taken a lot of reference to the above url

But all the results are printed like <span>4</span>/5. I don't want it to be in print.

Is there a good solution? thanks.

Share Improve this question asked Aug 8, 2019 at 4:36 Kt HKt H 152 silver badges6 bronze badges 4
  • can you share your actual code? – Shir Gans Commented Aug 8, 2019 at 5:05
  • @ShirGans It's not my code. I'm not sure if I can share the entire code because it's a code for a paid plug-in template. Is there no problem sharing? – Kt H Commented Aug 8, 2019 at 5:31
  • 1 No, better not to share the entire code. First you better not change the original plugin code, since it's not a good practice at all. All you changes should be made on your own template (or child template) using hooks and filters. This way your changes will not be erased. Anyway, could you share what code gave you the result of <span>4</span>/5 ? – Shir Gans Commented Aug 8, 2019 at 5:35
  • i tried $reviews_label = sprintf( __( '<span>%s</span> / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 ); and $reviews_label = <span> . sprintf( __( '%s</span> / %d', 'woocommerce-product-reviews-pro' ), $average_rating, 5 ); But it doesn't work the way I want it to. – Kt H Commented Aug 8, 2019 at 6:01
Add a comment  | 

1 Answer 1

Reset to default 1

Actaully using esc_html won't help because it will escape any html character, so you have to depend on wp_kses so you can specify the accepted tags and there attributes, so no need then to escape the output. but you have to escape the values comming from DB, which in you case $average_rating. so your can be like this.

$accepted_tags  = array('strong'=>array());

$reviews_label  = wp_kses(
                        sprintf( 
                            __( '%s / %d', '<strong>woocommerce-product-reviews-pro</strong>' ), 
                            esc_html($average_rating), 
                            5 
                        ), 
                    $accepted_tags );

<h3><?php echo $reviews_label ; ?></h3>

Update

As i can see from your comment, You need to add tag to the %s, so you need to just change the $accepted_tags to:

$accepted_tags  = array('span'=>array());

anf the sprintf to:

sprintf( 
        __( '<span>%s</span> / %d', 'woocommerce-product-reviews-pro' ), 
        esc_html($average_rating), 
        5 
    ),

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745260501a4619190.html

相关推荐

  • php - how can i add class or span tag at sprintf?

    i hope to add span tag or class at sprintfI want to enlarge the text size of the numbers in front of me. So I want to ad

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信