Overwrite text in a complicated filter hook
Pregunta
I am very new to PHP and WordPress. I have learned only the basics of filter hooks but this one seems to be a bit complicated. I want to replace the text This action will let you pay a deposit of
and for this product
preserving the value that is passed to %s
. I am hoping that someone will be able to help me out. Thank you.
<?php
echo wp_kses_post( apply_filters( 'yith_wcdp_deposit_only_message', sprintf( __( 'This action will let you pay a deposit of <span class="deposit-price">%s</span> for this product', 'yith-woocommerce-deposits-and-down-payments' ), wc_price( $deposit_value ) ), $deposit_value ) );
?>
Solución
It's easier to understand if you break it down. The code in your question is exactly the the same as this:
$message = sprintf( __( 'This action will let you pay a deposit of <span class="deposit-price">%s</span> for this product', 'yith-woocommerce-deposits-and-down-payments' ), wc_price( $deposit_value ) );
$message = apply_filters( 'yith_wcdp_deposit_only_message', $message, $deposit_value );
echo wp_kses_post( $message );
If you look at it that way, you can see that the yith_wcdp_deposit_only_message
filter is applied after the %s
has been substituted with wc_price( $deposit_value )
.
However, you can also see that the filter receives the $deposit_value
variable as its second argument, meaning that you can use it in your filter:
<?php
add_filter(
'yith_wcdp_deposit_only_message',
function( $message, $deposit_value ) {
$message = 'This is my new message, and the deposit value is ' . wc_price( $deposit_value );
return $message;
},
10,
2 // MUST be 2 to be able to use $deposit_value.
);