-
-
Save wpn/54e3aecc2ff57af8a852 to your computer and use it in GitHub Desktop.
ninja_forms_register_field
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 | |
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