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']
	GFCommon::log_debug( __METHOD__ . '(): HTTP_X_FORWARDED_FOR value: ' . $_SERVER['HTTP_X_FORWARDED_FOR'] );
	return $_SERVER['HTTP_X_FORWARDED_FOR'];
}

2. Prevent the IP address being saved

add_filter( 'gform_ip_address', '__return_empty_string' );

3. Get the visitor IP from CloudFlare

According to CloudFlare’s official documentation, the client (visitor) IP address is passed using the CF-Connecting-IP header. Some threads in their community forums point to a different header, HTTP_CF_CONNECTING_IP, that is not listed in Cloudflare’s documentation but seems to work for some people. The snippet below will add support for both headers to Gravity Forms.

add_filter( 'gform_ip_address', 'cloudflare_gform_ip_address' );
function cloudflare_gform_ip_address( $ip ) {
	// Return the IP address provided by CloudFlare.
	if ( isset( $_SERVER['CF-Connecting-IP'] ) && ! empty( $_SERVER['CF-Connecting-IP'] ) ) {
		$ip = isset( $_SERVER['CF-Connecting-IP']);
		GFCommon::log_debug( __METHOD__ . '(): CF-Connecting-IP: ' . $ip );
	} elseif ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) && ! empty( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
		$ip = isset( $_SERVER['HTTP_CF_CONNECTING_IP']);
		GFCommon::log_debug( __METHOD__ . '(): HTTP_CF_CONNECTING_IP: ' . $ip );
	}

	return $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.