0

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 ) );
?>
0

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.
);
1
  • Thank you for not only answering my question but also breaking down each section and explaining the parts I thought were very complex. I learned a lot from your answer. Trying out programming career after the harsh situation due to lock down. Thank you once again.
    – Swastika R
    Mar 31 '21 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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