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.
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.
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.
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?
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.
Perfect. It works like a champ!
Thanks, Christian.
Close thread.