Register Hidden Fields On Ninja Forms
Join the DZone community and get the full member experience.
Join For FreeWhen you are building a website, one of the most important areas of your site are forms, allowing you to capture data about your audience it can help you know what they want from you. There is no built in way in WordPress to create forms for your site, but there are a few really good plugins that allow you to do this.
The one I've been using recently is a plugin called Ninja Forms, it allows you to create forms in the admin area by dragging the fields you want into position on the form and display them in either the sidebar or content area of your site.
In this tutorial we are going to look at creating custom fields to the form so that you can collect more information about the user such as IP address and the page the form was filled out.
Create New Field Elements
There is a function that is included in Ninja Forms called ninja_forms_register_field that allows you to programmatically create new fields that you can use on the drag and drop interface to add onto your form.
The following code will use this function to create two new fields one to collect the IP address and the other to collect the page the form was filled out on. The important parameter to note here is the display_function, this will define the callback function to display the form element.
function register_ninja_form_fields() { $argsIp = array( 'name' => 'User IP', 'display_function' => 'collect_user_ip_display', 'sidebar' => 'template_fields', 'display_label' => false, 'display_wrap' => false, ); $argsPage = array( 'name' => 'Page', 'display_function' => 'collect_user_page', 'sidebar' => 'template_fields', 'display_label' => false, 'display_wrap' => false, ); if( function_exists( 'ninja_forms_register_field' ) ) { ninja_forms_register_field('user_ip', $argsIp); ninja_forms_register_field('user_page', $argsPage); } } add_action( 'init', 'register_ninja_form_fields' );
Next we need to create the functions which are used in the callback when creating the fields for the form.
Create User ID Element
Below is the code you need to display the hidden field that is the User IP address. We need to do a check to see if the field is on a page or not, this is because in the backend of the form area you can display the submission values for the form. The problem you will have is because we are using hidden fields you won't be able to see the IP address when looking at the form submissions, therefore we need to change the field to a text box in the admin area. The way we will do this is by checking if the global $post variable is set, if it isn't set then we can display the form in a text box.
function collect_user_ip_display( $field_id, $data ) { global $post; $id = $_SERVER["REMOTE_ADDR"]; if(!empty($post)) { ?> <input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $id;?>"> <?php } if(is_admin()) { ?> <div class="field-wrap text-wrap label-above"> <label for="ninja_forms_field_<?php echo $field_id;?>">User IP</label> <input type="text" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $data['default_value'];?>"> <p><a href="http://whois.domaintools.com/<?php echo $data['default_value'];?>" target="_blank">Find out more about this person</a></p> </div> <?php } }
Create To User Page
The next function is the callback function used to record the current page the form is on, this is so if you want to use the same form on multiple pages you will know what page this form was submitted from. This is very useful if you have a sign up form with an event and need to know what event people are signing up for.
If this we use the $post variable and search for the the permalink from using the post ID.
function collect_user_page( $field_id, $data ) { global $post; if(!empty($post)) { ?> <input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo get_permalink($post->ID);?>"> <?php } if(is_admin()) { ?> <div class="field-wrap text-wrap label-above"> <label for="ninja_forms_field_<?php echo $field_id;?>">Page Form Submitted</label> <input type="text" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $data['default_value'];?>"> </div> <?php } }
Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments