How to Store the Non-Cached IP Address Into a Hidden Field

Introduction

Would you like to store the non-cached IP address in a Hidden Field with WPForms?

You can use the {user_ip} Smart Tag to pull in the IP address of the user completing your form. For further information on that, please see this article.

However, this IP address is captured at the time the user loads the page and could very well be a cached version.

When using this snippet, you won’t need to use the user_ip Smart Tag. This snippet will automatically get the user’s IP for you and place it in the Hidden Field while ensuring that the number captured will not be the cached version of the IP address.

For help in understanding what an IP address is, check out this description from our friends at WPBeginner.

Creating your form

First, you’ll need to create your form and add the fields that you need.

If you need any help in creating your form, please see this documentation.

Once you’ve added all the necessary fields, add the Hidden Field to your form and save the form.

adding a hidden field to capture the non-cached ip address

Adding the snippet to capture the non-cached IP address

Now it’s time to add the snippet to your site.

If you need any help in how to add snippets to your site, please see this tutorial.

/**
 * Do not store the cached IP address in a hidden field
 *
 * @link   https://wpforms.com/developers/how-to-store-the-non-cached-ip-address-into-a-hidden-field/
 */

function wpf_hidden_ip_avoid_cache( $fields, $entry, $form_data ){

    // Only run on the form with ID 727
    if( $form_data[ 'id' ] == 727 ) {

		//Look for the hidden field we want to replace the value of
		foreach( $fields as $field_id => $field ){
			
			// Look for field ID is 10 - it MUST be a Hidden Field, change this ID to match your field ID
			if( '10' == $field[ 'id' ] && 'hidden' == $field[ 'type' ] ){ 
				
				// Replace that value with the IP WPForms detects
				$fields[ $field_id ][ 'value' ] = wpforms_get_ip(); 
			}
		}
    }

	return $fields; 
}
add_filter( 'wpforms_process_filter', 'wpf_hidden_ip_avoid_cache', 10, 3 );

The snippet above will only run on the form ID 727, if the field ID of 10 is indeed a Hidden Field, it will update this field after the form is submitted with the user’s IP address.

If you need any help in finding your form and field IDs, please check out this tutorial.

without using a Smart Tag, you can easily capture the IP address after the form is submitted to make sure the address that is recorded is a non-cached address

And that’s it! Are you looking to add some CSS animation to your form’s confirmation message? Check out our tutorial on How to Add Falling Autumn Leaves to Your Confirmation Message.

Filter Reference: wpforms_process_filter