Skip Navigation

[Resolved] Can you capture IP data using Forms?

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

Problem:
How to capture the IP address of users who submit posts with a Toolset Form?

Solution:
You would need to add a little code that uses the cred_save_data hook to run after your form is submitted, like so:

function tssupp_record_ip( $post_id, $form_data ){
 
    if ( in_array( $form_data['id'], array( 123, 234 ) ) ) { // Edit Form IDs
 
        // source: https://www.codexworld.com/how-to/get-user-ip-address-php/
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            //ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            //ip pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $ip = $_SERVER['REMOTE_ADDR'];
        }
 
    update_post_meta( $post_id, '_userip', $ip );  
 
    }
}
add_action( 'cred_save_data', 'tssupp_record_ip', 10, 2 );

In this example the IP address is saved to a hidden field, but you may want to save it to a generic or a Types field.

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

This support ticket is created 6 years, 4 months 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 7 replies, has 3 voices.

Last updated by thoraldM 6 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1112535

Like, say I have a form where users submit content.

Is it possible to capture the IP address of the device which they had used to make the submission?

#1112772

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Yes, the IP address is available in the http request information accessible in PHP from the $_SERVER global variable.

You can read about that, including how to get the "real" IP address, here: https://www.codexworld.com/how-to/get-user-ip-address-php/

The question would be how best to use this data.

If users are submitting posts via a form, and you would like to store the IP address of the user that submitted the post as a custom field of that post, then you would include the field (e.g. "user-ip") in your Form, and then you could create a custom shortcode that returns the IP address using code such as that in the linked article to set the field value.

The alternative would be not to include the field in the form, and to use a hook such as cred_save_data to do some server-side processing when the form was submitted to store the IP address somewhere that makes sense.

I might be able to help if you give details about where you want to save this data.

#1112955

Hello Nigel,

First of all, I want to say thanks to your developers for releasing the new version of Toolset Forms. Whatever they did, it fixed the issues I was having with the jQuery not loading on the chart posts.

Also, the new Types also fixed an unrelated issue of non-post/post type items (for example, templates for Views), showing up on the site RSS feed.

So, good job you guys!

Now, back to the IP data capture... I think I would prefer not putting it on the form itself. I just want to be able to capture the data and come back to it later if needed (for security reasons). So I'm thinking of just displaying the data in the back end associated with the content submitted on the front end. How do I go about doing that?

#1113051

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You'll still want to save the IP address as a custom field, but not a Types field (which is shown on the post edit screen for the post), and to be extra sure, you can use a field slug (the meta key) beginning with an underscore, which means it is hidden and wouldn't be shown even if someone changed the settings to display the standard WordPress UI for custom fields on post edit screens.

So, you'll need some code that uses the cred_save_data hook, like so:

function tssupp_record_ip( $post_id, $form_data ){

	if ( in_array( $form_data['id'], array( 123, 234 ) ) ) { // Edit Form IDs

		// source: <em><u>hidden link</u></em>
		if(!empty($_SERVER['HTTP_CLIENT_IP'])){
			//ip from share internet
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
			//ip pass from proxy
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}else{
			$ip = $_SERVER['REMOTE_ADDR'];
		}

	update_post_meta( $post_id, '_userip', $ip );  

	}
}
add_action( 'cred_save_data', 'tssupp_record_ip', 10, 2 );

That should record the IP address in a hidden custom field "_userip" against each post submitted by the Toolset Forms you specify (edit for the Form IDs).

Note that Types 3.1 added a code snippets section in Toolset > Settings where you can add such code without editing your theme files.

#1113143

Thanks Nigel. I tried that code you gave me, and I think I did it right as I didn't encounter any errors. However, when I load the post automatically created by Forms in the WordPress editor, I can't find the value of the user IP in the custom fields section. Is it because it's hidden? Does it mean I would have to go to the database level (phpmyadmin) to look up the data?

#1113431

Hi Nigel

I'm interested in this too; could you unhide the link here please (I don't believe it contains any info belonging to @thoraldM)? https://toolset.com/forums/topic/can-you-capture-ip-data-using-forms/#post-1112772. Thanks

#1114351

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The sample code stores the IP address in a hidden field "_userip", which you can retrieve from the database using get_post_meta and which you can output using wpv_post_field, for example.

But if you want to see it on the post edit screen in the custom fields section then it shouldn't be a hidden field, meaning you should use a field slug that doesn't begin with an underscore (e.g. just "userip"). Sorry, I think I interpreted your question as wanting to keep it hidden.

(Julie you should be able to see that link now.)

#1114444

My issue is resolved now. Thank you!