Last active
May 27, 2019 10:28
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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