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 |1 Answer
Reset to default 1Actaully 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
$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