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.
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?
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.
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?
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.