Home / Admin / Storing User’s Uncached IP Address in Hidden Field
Duplicate Snippet

Embed Snippet on Your Site

Storing User’s Uncached IP Address in Hidden Field

This snippet lets you capture and store the user's IP address in the Hidden field. It modifies the default behavior of the {user_ip} Smart Tag to ensure the IP address is captured at the exact location and not a cached version.

<10
Code Preview
php
<?php
/**
 * 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 );

Comments

Add a Comment