Using Gravity Forms ‘gform_currency_pre_save_entry’ PHP action

The gform_currency_pre_save_entry filter allows you to override the default currency code before the entry is saved.

Usage

A generic example for all forms:

add_filter('gform_currency_pre_save_entry', 'your_function_name', 10, 2);

To target a specific form, append the form ID to the hook name (format: gform_currency_pre_save_entry_FORMID):

add_filter('gform_currency_pre_save_entry_5', 'your_function_name', 10, 2);

Parameters

  • $currency (string): The three-character ISO currency code to be stored in the entry. Default is the value returned by GFCommon::get_currency().
  • $form (Form Object): The form currently being processed.

More information

See Gravity Forms Docs: gform_currency_pre_save_entry

Examples

Override currency code with a value from a form field

This code snippet allows you to change the currency code based on a value from a form field.

add_filter('gform_currency_pre_save_entry', 'override_currency_code', 10, 2);
function override_currency_code($currency, $form) {
    return rgpost('input_7');
}

Set currency code based on user’s country

This code snippet sets the currency code based on the user’s country, using a field with an ID of ‘input_5’.

add_filter('gform_currency_pre_save_entry', 'set_currency_based_on_country', 10, 2);
function set_currency_based_on_country($currency, $form) {
    $user_country = rgpost('input_5');
    if ($user_country == 'US') {
        return 'USD';
    } elseif ($user_country == 'UK') {
        return 'GBP';
    } else {
        return 'EUR';
    }
}

Force a specific currency for a specific form

This code snippet forces a specific currency (USD) for a specific form with the ID of 3.

add_filter('gform_currency_pre_save_entry_3', 'force_usd_currency', 10, 2);
function force_usd_currency($currency, $form) {
    return 'USD';
}

Set the currency based on a dropdown selection

This code snippet sets the currency based on a dropdown selection with an ID of ‘input_10’.

add_filter('gform_currency_pre_save_entry', 'set_currency_based_on_dropdown', 10, 2);
function set_currency_based_on_dropdown($currency, $form) {
    $selected_currency = rgpost('input_10');
    return $selected_currency;
}

Set currency based on user role

This code snippet sets the currency based on the current user’s role.

add_filter('gform_currency_pre_save_entry', 'set_currency_based_on_user_role', 10, 2);
function set_currency_based_on_user_role($currency, $form) {
    $current_user = wp_get_current_user();
    if (in_array('administrator', $current_user->roles)) {
        return 'USD';
    } else {
        return 'EUR';
    }
}

Place the code in the functions.php file of your active theme.