Skip Navigation

[Resolved] Capturing a submitters IP address on Post Form

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to automatically set a custom field in a post created by Forms. The value should be the User's IP address.

Solution: Create a custom field in Types that will hold the IP address. Remove the field from the Form builder. Use the cred_save_data hook to automate the custom field value.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 5 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1448969

Within custom_functions.php I entered the following (our form ID=1957):

add_action('cred_save_data', 'save_ip_address',10,2);
function save_ip_address($post_id, $form_data)
{
// if a specific form
    if ($form_data['id']==1957)
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        //The IP address from which the user is viewing the current page. 
 
        update_post_meta($post_id, 'wpcf-ip-address', $ip);
        //Update post meta field
    }
}

I assumed I needed to add the following shortcode to the form itself:
User IP Address: [wpcf-ip-address]
however theh form just echos back the above line w/ no IP address.

#1449387

Hello, can you tell me more about what you want to accomplish?

The cred_save_data hook is triggered when a Form is submitted, not when a new post Form is displayed. So if your Form is used to create a new post, the wpcf-ip-address field information is not actually saved in the post until the Form has been submitted. Did you create a custom field in Types to hold the IP address? If so, you could display that IP address in a post template using a Types field shortcode: https://toolset.com/documentation/customizing-sites-using-php/functions/
Click "+More" under any field type to display example codes.

[wpcf-ip-address]

There is no shortcode wpcf-ip-address, so this line won't do anything. If you want to display a Types field in a post, you can use the Types field shortcode. If you want to display the User's current IP in a new post form, then you need to create a custom shortcode that returns the User's IP address. If you want to save that IP address along with the post, then you should create a Types field to hold the IP. So I'm not really clear what you want to achieve here. If you want to provide more information, then I can provide some additional guidance.

#1449523

Thanks Christian. Yes when a user submits a form to me (Footer Contact Form), and thus a new Post is created, I want our Email Notification Form to display that submitters IP address. To your idea, I like that it could save their IP in the Post.

It sounds like I need to create an 'IP Address' field in our existing ‘Footer Contact Form Fields’ Field Group, so that it can hold/store the users IP Address in the Post.

I realize I must add this cred-field to the Post Form, but confused as I don’t want it to show on the front-end, or want the user to self-input their own IP.

Does the code I mentioned above capture the users IP?
Would then I just need something to force it into the cred-field when a user submits?

#1449599

It sounds like I need to create an 'IP Address' field in our existing ‘Footer Contact Form Fields’ Field Group, so that it can hold/store the users IP Address in the Post.
Yes, I think you are correct here. In the code above, the field slug is "ip-address". You can reuse that in your field, or update the code to reflect your chosen field slug.

I realize I must add this cred-field to the Post Form, but confused as I don’t want it to show on the front-end, or want the user to self-input their own IP.
If you do not want to show the field in the Form, delete the field from the Form Builder. It's not necessary in the Form. The cred_save_data code you have above will automatically set the field value when the Form is submitted, without the need to display it in the Form itself.

Does the code I mentioned above capture the users IP?
I'm not sure what is the best way to get a User's IP, that's not something Toolset is designed to accomplish. I did a quick search online and saw something similar, but I can't guarantee it is the best way. In theory if that line does get the User's IP, the code you shared does indeed store it in a custom field on the post.

#1449607

Perfect. It works like a champ!
Thanks, Christian.
Close thread.

#1449611

Closing