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 ) );
?>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0