I'm using the Give plugin and am trying to add an option for the donor to opt-in to pay an extra percentage. I'm trying to set a conditional filter based on whether or not this option is checked when the user tries to submit a donation.


//Use Ajax to see if user checked fee box before submitting
        function check_for_fees(){
            var checked;
            //I have a checkbox in my HTML with the id "#gateway-fee"
            else {
                type: "POST",
                url: ajaxurl,
                data: {
                    action: 'give_apply_fee',
                    checked: checked
                error: function(){
                    alert("Still not working!")
                success: function(data){
                    alert('Got this from the server: ' + data);



add_action('wp_ajax_give_apply_fee', 'give_apply_fee');
add_action('wp_ajax_nopriv_give_apply_fee', 'give_apply_fee');

//apply fee 
function give_apply_fee(){
    $checked = $_POST['checked'];
    echo $checked; //Make sure I'm returning "true" or "false"
    if($checked == true){// Calculate new checkout cost if checked
        function give_basic_fees_add_fee_true($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;
        // Use this filter if "#gateway-fee" is checked
        add_filter( 'give_donation_total', 'give_basic_fees_add_fee_true', 1, 1 );
    else {//Keep original value
        function give_basic_fees_add_fee_false($sanitized_amount){
            return $sanitized_amount;
        // Use this filter if "#gateway-fee" is NOT checked
        add_filter( 'give_donation_total', 'give_basic_fees_add_fee_false', 1, 1 );

Is trying to conditionally load which filter is used this way via Ajax possible?

Regardless of if the checkbox is checked or not, the submitted donation is the original amount.

I'm basing my code off of https://github.com/WordImpress/Give-Snippet-Library/blob/master/form-customizations/basic-fees.php. This plugin does not give users the option to opt in to the fee, which is what I'm trying to implement.

  • A filter persists for the lifetime of the request it's added on. Your ajax request and the form submission request are two separate http requests. – Milo Apr 20 '17 at 0:34
  • Do you have a recommendation on how to achieve the functionality I'm aiming for then, with or without ajax? The issue I'm encountering seems to be that my $checked variable doesn't get carried over to my added give_donation_total filter – user3183717 Apr 20 '17 at 0:51

Your Answer

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

Browse other questions tagged or ask your own question.