gform_ip_address

Description

Allows the IP address of the client to be modified. Use this filter if the server is behind a proxy.

Usage

add_filter( 'gform_ip_address', 'your_function_name' );

Parameters

  • $ip string

    The IP being used.

Examples

1. Use HTTP_X_FORWARDED_FOR

Use this 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'];
}

2. Prevent the IP address being saved

add_filter( 'gform_ip_address', '__return_empty_string' );

3. 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.
	// previous version: return $_SERVER['CF-Connecting-IP'];
	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.