Call WordPress filter action with Ajax

I’m using the Give plugin for WordPress. They have a proof-of-concept pluginn that allows adding a fee at checkout

This works as expected and have no problems there, but what I’m trying to do is make the fee optional so the donor can choose from the front-end whether or not they want to add that fee.

Here’s the part of the code that creates the new donation amout:

function give_basic_fees_add_fee( $sanitized_amount ) {
    $fee_percentage = (int) give_get_option( 'basic_fee_percentage' );
    $fee            = $sanitized_amount * ( $fee_percentage / 100 );
    $new_total      = $fee + $sanitized_amount;
    return $new_total;
add_filter( 'give_donation_total', 'give_basic_fees_add_fee', 1, 1 );

What I’m trying to do is call this with Ajax. I tried the below, which is not working:


//Use Ajax to see if user checked fee box before submitting
//I've already confirmed this part of the script is loading in the footer.
            function check_for_fees(){
                var checked;
                else {
                var data = {
                    'action': 'give_basic_fees_add_fee',
                    'checked': checked
                $.post(ajaxurl, data, function(response) {
                    alert('Got this from the server: ' + response);


add_action('wp_axaj_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
add_action('wp_axaj_nopriv_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
function give_basic_fees_add_fee( $sanitized_amount ) {

    /*I just want to alert "Checked" so I know it's working, and create
    additional conditional code depending on the value of "checked".*/
    $checked        = intval($_POST['checked']);
    echo $checked;


Read more here: Call WordPress filter action with Ajax

Leave a Reply

Your email address will not be published. Required fields are marked *