Conditional Gravity Forms filter in WordPress Multisite

I'm using this technique: https://stackoverflow.com/questions/27323460/how-to-label-payments-in-gravity-forms-paypal-pro

To add comments to the paypal comments field from a gravity form. However, it's a multisite configuration. I'd like to modify it so the filter only triggers on blog_id 2

What's the proper method to make this conditional?

Much thanks.

Topic conditional-tags functions multisite Wordpress

Category Web


Using the code from the answer you linked in your question (untested at present):

add_filter( 'gform_paypalpaymentspro_args_before_payment','add_donation_comment', 10, 2 );
function add_donation_comment( $args, $form_id ) { // do not edit $args or $form_id
    global $blog_id; // Make this available to the function

    // Apply filter only if blog id is 2
    if ( 2 !== $blog_id ) {
        return $args;
    }       

    // apply to form 1 only
    if ( 1 == $form_id ) { // Replace '1' with your form id
            $args["COMMENT1"] = 'Field Project'; // Comment1 is the arg that paypal pro uses. 
    }
    // apply to form 2 only
    if ( 2 == $form_id ) { // Replace '2' with your form id
            $args["COMMENT1"] = 'Help Us Grow';
    }
    // always return the args, even if not changing anything 
    return $args;
}

Adding the global $blog_id line makes this WP value available to the function. As you know, it stores the site ID# in a multi-site install, allowing you to apply your filter on specific sites in your network.

The second addition is just a test of that value, written in the form of a Yoda condition. If this function is executing on any other site, the $args array is returned unchanged.

The rest of this code is exactly as found in the answer you referenced.

NOTE: I am unable to test this right now but I will be available later if you have any problems. Cheers.


Based on @jdm2112 response, I did the following:

add_filter('gform_paypalpaymentspro_args_before_payment','add_donation_comment', 10, 2 );
function add_donation_comment( $args, $form_id ) { // do not edit $args or $form_id
    global $blog_id; // Make this available to the function

    // Apply filter only if blog id is 2
    if ( 2 == $blog_id ) {

        // apply to form 1 only
        if ( 1 == $form_id ) { // Replace '1' with your form id
                $args["COMMENT1"] = 'Field Project'; // Comment1 is the arg that paypal pro uses. 
        }
        // apply to form 2 only
        if ( 2 == $form_id ) { // Replace '2' with your form id
                $args["COMMENT1"] = 'Help Us Grow';
        }
        // always return the args, even if not changing anything 
        return $args;

    }       

    else {

        // always return the args, even if not changing anything 
        return $args;

    }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.