0

Suppose i am developing a plugin that is related too booking.Next time i want to add an add-ons on that plugin for payment.Then i added another add-ons for paypal payment.

Assume that below is the payment gateway UI interface in html

<div class="payments-options">
    <div class="bank-payment"></div>
    <div class="cash-on-delivery"></div>
    <!--- Here i want to add a new div for paypal same as other two ---->
</div>

So how can i use add_filters() , apply_filter(), add_action() and do_action() Here to achieve this?

1
  • Have you read this guide about using filters and hooks?
    – Johansson
    Commented Jun 12, 2018 at 18:47

1 Answer 1

0

It's a very broad question, but my approach would look something like this:

I'd replace your code with this:

<?php my_the_payment_options(); ?>

And then in my plugin:

function my_the_payment_options() {
    $payment_options = get_my_available_payment_options();
    if ( $payment_options ) :
    ?>
    <div class="payments-options">
        <?php foreach ( $payment_options as $k => $v ) call_user_func( $v['callback'] ); ?>
    </div>
    <?php
    endif;
}

function get_my_available_payment_options() {
    return apply_filters( 'get_my_available_payment_options', array() );
}

and then in code adding payment method (here for PayPal):

function my_add_paypal( $available_payments ) {
    $available_payments[ 'paypal' ] => array(
        'callback' => 'my_paypal_callback',
        // some other params if needed
    );
}
add_filter( 'get_my_available_payment_options', 'my_add_paypal' );

function my_paypal_callback() {
    ?>
    <div class="paypal-div"></div>
    <?php
}

Disclaimer: Treat this code as way to show the idea and not as working solution :)

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.