Using Gravity Forms ‘gform_ip_address’ PHP filter

The gform_ip_address filter in Gravity Forms PHP allows you to modify the IP address of the client. This is useful when your server is behind a proxy.

Usage

add_filter('gform_ip_address', 'your_function_name');

Parameters

  • $ip (string): The IP address being used.

More information

See Gravity Forms Docs: gform_ip_address

Examples

Use HTTP_X_FORWARDED_FOR

If you are behind a proxy, this example will use HTTP_X_FORWARDED_FOR, allowing the visitor IP to be displayed instead of the proxy IP. This is just an example valid for a common scenario, if it’s not working for you, contact your host support for assistance.

add_filter('gform_ip_address', 'filter_gform_ip_address');
function filter_gform_ip_address($ip) {
    // Return the IP address set by the proxy.
    // E.g. $_SERVER['HTTP_X_FORWARDED_FOR'] or $_SERVER['HTTP_CLIENT_IP']
    return $_SERVER['HTTP_X_FORWARDED_FOR'];
}

Prevent the IP address being saved

add_filter('gform_ip_address', '__return_empty_string');

Use CloudFlare’s HTTP_CF_CONNECTING_IP header

According to CloudFlare’s documentation, the client (visitor) IP address is passed using the CF-Connecting-IP header. Use the snippet below to make Gravity Forms use this HTTP header.

add_filter('gform_ip_address', 'cloudflare_gform_ip_address');
function cloudflare_gform_ip_address($ip) {
    // Return the IP address provided by CloudFlare's HTTP_CF_CONNECTING_IP header.
    return $_SERVER['HTTP_CF_CONNECTING_IP'];
}

Placement

This code should be placed in the functions.php file of your active theme.

Since

This filter was added in Gravity Forms v2.2.

Source Code

This filter is located in forms_model.php.