0

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.com/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.

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

1 Answer 1

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 
    ),
2
  • it worked perfectly. I don't understand what happened. I've saved a lot of time thanks to you. And I learned a lot !
    – Kt H
    Commented Aug 8, 2019 at 7:09
  • Glad I could help, you are welcome
    – Makiomar
    Commented Aug 8, 2019 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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