Skip to content

Instantly share code, notes, and snippets.

@wpn
Created January 16, 2014 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpn/25b50637a5d518f1abac to your computer and use it in GitHub Desktop.
Save wpn/25b50637a5d518f1abac to your computer and use it in GitHub Desktop.
Capturing The IP Address of a Form Submitter
<?php
// Register our IP collection field.
// Because our field doesn't have any backend editing options, we only need to specify a few things.
function register_ip_field() {
$args = array(
'name' => 'User IP', // This will be the label of the field button in the back-end editor.
'display_function' => 'collect_user_ip_display', // This function will be called when the form is rendered on the front-end.
'sidebar' => 'template_fields', // This is the sidebar on the Field Settings tab that this field will show up in.
'display_label' => false, // Since we're adding a hidden form, we don't want to show the label on the front-end.
'display_wrap' => false, // Again, this is a hidden field, so we don't need the div wrapper that's normally output to the front-end.
);
if( function_exists( 'ninja_forms_register_field' ) ) {
ninja_forms_register_field('user_ip', $args);
}
}
add_action( 'init', 'register_ip_field' );
<?php
// The only thing we have left to do is create the function that will output the HTML for the field when the form is rendered.
/*
* This function only has to output the specific field element. The wrap is output automatically.
*
* $field_id is the id of the field currently being displayed.
* $data is an array the possibly modified field data for the current field.
*
*/
function collect_user_ip_display( $field_id, $data ){
$ip = $_SERVER["REMOTE_ADDR"]; // Get our user's IP address.
// The "name" attribute below is very important. It has to be ninja_forms_field_$field_id
?>
<input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $ip;?>">
<?php
}
<?php
// Register our IP collection field.
// Because our field doesn't have any backend editing options, we only need to specify a few things.
function register_ip_field() {
$args = array(
'name' => 'User IP', // This will be the label of the field button in the back-end editor.
'display_function' => 'collect_user_ip_display', // This function will be called when the form is rendered on the front-end.
'sidebar' => 'template_fields', // This is the sidebar on the Field Settings tab that this field will show up in.
'display_label' => false, // Since we're adding a hidden form, we don't want to show the label on the front-end.
'display_wrap' => false, // Again, this is a hidden field, so we don't need the div wrapper that's normally output to the front-end.
);
if( function_exists( 'ninja_forms_register_field' ) ) {
ninja_forms_register_field('user_ip', $args);
}
}
add_action( 'init', 'register_ip_field' );
/*
* This function only has to output the specific field element. The wrap is output automatically.
*
* $field_id is the id of the field currently being displayed.
* $data is an array the possibly modified field data for the current field.
*
*/
function collect_user_ip_display( $field_id, $data ){
$ip = $_SERVER["REMOTE_ADDR"]; // Get our user's IP address.
// The "name" attribute below is very important. It has to be ninja_forms_field_$field_id
?>
<input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $ip;?>">
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment