The gform_payment_methods Gravity Forms PHP filter allows you to add alternative payment methods to the Card field.
Usage
add_filter('gform_payment_methods', 'your_function_name', 10, 3);
Parameters
- $payment_methods (array) – Array containing other loaded payment methods.
- $field (Field Object) – The current field.
- $form_id (integer) – The ID of the current form.
More information
See Gravity Forms Docs: gform_payment_methods
Note that we recommend using the payment field specific to the payment type you are using, such as Stripe Card Field or PayPal Field. The Card Field is intended as an option for payment methods where no integrated payment field is available.
Examples
Add Test Payment method
This example adds a payment method named Test Payment.
add_filter('gform_payment_methods', 'add_payment', 10, 3); function add_payment($payment_methods, $field, $form_id) { $payment_methods[] = array('key' => 'testing', 'label' => 'Test Payment'); return $payment_methods; }
Add Custom Payment method
This example adds a custom payment method called “Custom Payment” to the form with ID 1.
add_filter('gform_payment_methods', 'add_custom_payment', 10, 3); function add_custom_payment($payment_methods, $field, $form_id) { if ($form_id == 1) { $payment_methods[] = array('key' => 'custom', 'label' => 'Custom Payment'); } return $payment_methods; }
Remove existing payment methods and add a new one
This example removes all existing payment methods and adds a new payment method called “Exclusive Payment” to the form with ID 2.
add_filter('gform_payment_methods', 'replace_payment_methods', 10, 3); function replace_payment_methods($payment_methods, $field, $form_id) { if ($form_id == 2) { $payment_methods = array(array('key' => 'exclusive', 'label' => 'Exclusive Payment')); } return $payment_methods; }
Modify existing payment method label
This example modifies the label of the existing payment method with the key ‘testing’ to “Modified Test Payment”.
add_filter('gform_payment_methods', 'modify_payment_method_label', 10, 3); function modify_payment_method_label($payment_methods, $field, $form_id) { foreach ($payment_methods as &$payment_method) { if ($payment_method['key'] == 'testing') { $payment_method['label'] = 'Modified Test Payment'; } } return $payment_methods; }
Add payment method based on user role
This example adds a payment method called “Special Payment” only for users with the ‘administrator’ role.
add_filter('gform_payment_methods', 'add_payment_based_on_role', 10, 3); function add_payment_based_on_role($payment_methods, $field, $form_id) { if (current_user_can('administrator')) { $payment_methods[] = array('key' => 'special', 'label' => 'Special Payment'); } return $payment_methods; }