Skip to content

Instantly share code, notes, and snippets.

@wpn
Created January 16, 2014 18:17
Show Gist options
  • Save wpn/54e3aecc2ff57af8a852 to your computer and use it in GitHub Desktop.
Save wpn/54e3aecc2ff57af8a852 to your computer and use it in GitHub Desktop.
ninja_forms_register_field
<?php
function register_my_custom_field() {
$args = array(
'name' => 'My Custom Field',
'edit_options' => array(
array(
'type' => 'text',
'name' => 'my_text',
'label' => 'My Text Label',
'class' => 'widefat',
),
array(
'type' => 'select',
'name' => 'my_select',
'label' => 'My Select Label',
'options' => array(
array( 'name' => 'Item 1', 'value' => 'item_1' ),
array( 'name' => 'Item 2', 'value' => 'item_2' ),
array( 'name' => 'Item 3', 'value' => 'item_3' ),
),
),
),
'display_function' => 'my_new_field_display',
'edit_function' => 'my_new_field_edit',
'sidebar' => 'template_fields',
'pre_process' => 'my_new_field_pre_process',
'process' => 'my_new_field_process',
);
if( function_exists( 'ninja_forms_register_field' ) ) {
ninja_forms_register_field('my_field', $args);
}
}
add_action( 'init', 'register_my_custom_field' );
/*
* This function outputs HTML for the backend editor.
* The naming convention is: ninja_forms_field_4[custom_value].
* It will be available as $data[custom_value].
*
* $field_id is the id of the field currently being edited.
* $data is an array of the field data, including any custom variables.
*
*/
function my_new_field_edit( $field_id, $data ){
if( isset( $data['my_value'] ) ){
$my_value = $data['my_value'];
}else{
$my_value = '';
}
?>
<input type="text" name="ninja_forms_field_<?php echo $field_id;?>[my_value]" value="<?php echo $my_value;?>">
<?php
}
/*
* 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 my_new_field_display( $field_id, $data ){
if( isset( $data['my_value'] ) ){
$my_value = $data['my_value'];
}else{
$my_value = '';
}
?>
<input type="text" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $my_value;?>">
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment