Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active May 27, 2019 10:28
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 damiencarbery/0d9f3f837e42b7033a85814aac8685b6 to your computer and use it in GitHub Desktop.
Save damiencarbery/0d9f3f837e42b7033a85814aac8685b6 to your computer and use it in GitHub Desktop.
Ninja Forms - Copy Fields on Submission: Simplify copying Ninja Forms field values between fields early in the submission. https://www.damiencarbery.com/2019/05/ninja-forms-copy-data-during-submission/
<?php
/*
Plugin Name: Copy Email field to Hidden
Plugin URI: https://www.damiencarbery.com
Description: Copy the email field to the hidden field using the NinjaFormsCopyFieldsOnSubmission class.
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'nfcf_set_copy_fields', 'dcwd_copy_fields', 10, 3 );
function dcwd_copyfields( $copy_fields, $form_id, $form_data ) {
if ( 2 == $form_id ) {
$copy_fields[ 'email_address_field' ] = 'hidden_1558370856218';
}
// A different form.
if ( 5 == $form_id ) {
$copy_fields[ 'preferred_date_dropdown_asist' ] = 'preferred_date_hidden';
}
return $copy_fields;
}
<?php
/*
Plugin Name: Ninja Forms - Copy Fields on Submission
Plugin URI: https://www.damiencarbery.com
Description: Copy Ninja Forms field values during form submission.
Author: Damien Carbery
Version: 0.1
*/
class NinjaFormsCopyFieldsOnSubmission {
private static $instance;
private $copy_fields;
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new NinjaFormsCopyFieldsOnSubmission();
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
//$this->form_id = null;
$this->copy_fields = array();
//$this->fields_ids = array();
$this->init();
}
// Set up WordPress specfic actions.
public function init() {
add_filter( 'ninja_forms_submit_data', array( $this, 'copy_fields' ) );
}
public function copy_fields( $form_data ) {
$this->copy_fields[ 'copy_key_from' ] = 'copy_key_to'; // Initialise with sample data.
$this->copy_fields = apply_filters( 'nfcf_set_copy_fields', null, $form_data[ 'id' ], $form_data );
// Shortcut the function if there are no fields to copy in this form.
if ( empty( $this->copy_fields ) ) {
return $form_data;
}
// Store the field keys in an array with the field ID as the value.
$fields_ids = array();
foreach ( $form_data[ 'fields' ] as $field ) {
$fields_ids[ $field[ 'key' ] ] = $field[ 'id' ];
}
foreach ($this->copy_fields as $copy_from => $copy_to) {
// Only copy if both field keys are valid.
if ( array_key_exists( $copy_from, $fields_ids ) && array_key_exists( $copy_to, $fields_ids ) ) {
$form_data[ 'fields' ][ $fields_ids[ $copy_to ] ][ 'value' ] = $form_data[ 'fields' ][ $fields_ids[ $copy_from ] ]['value'];
}
}
return $form_data;
}
}
$NinjaFormsCopyFieldsOnSubmission = new NinjaFormsCopyFieldsOnSubmission();
<?php
$form_data = array (
'id' => '2',
'fields' =>
array (
5 =>
array (
'value' => 'My Name Is',
'id' => 5,
'key' => 'name_first_field',
),
6 =>
array (
'value' => 'damien@damiencarbery.com',
'id' => 6,
'key' => 'email_address_field',
),
7 =>
array (
'value' => 'Message is here.',
'id' => 7,
'key' => 'message_textarea_key',
),
8 =>
array (
'value' => '',
'id' => 8,
'key' => 'submit',
),
32 =>
array (
'value' => '',
'id' => 32,
'key' => 'hidden_1558370856218',
),
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment