DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Dynamic Web Forms In React For Enterprise Platforms
  • How to Get Word Document Form Values Using Java
  • Lightning Data Service for Lightning Web Components

Trending

  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • How to Create a Successful API Ecosystem
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Register Hidden Fields On Ninja Forms

By 
Paul Underwood user avatar
Paul Underwood
·
Mar. 29, 15 · Interview
Likes (0)
Comment
Save
Tweet
Share
7.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

ninja-forms

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.

Ninja Forms

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
	}
}


Form (document)

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Dynamic Web Forms In React For Enterprise Platforms
  • How to Get Word Document Form Values Using Java
  • Lightning Data Service for Lightning Web Components

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!