Last active
October 13, 2016 08:06
-
-
Save svenl77/3bec0bf79f0e9a002f0a to your computer and use it in GitHub Desktop.
BuddyForms New Form Element Example
This file contains hidden or 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 | |
// We need to add a new option to the form elements select | |
add_filter( 'buddyforms_add_form_element_to_select', 'my_form_element_to_select', 1, 2 ); | |
function my_form_element_to_select( $elements_select_options ) { | |
global $post; | |
if ( $post->post_type != 'buddyforms' ) { | |
return; | |
} | |
// Add the Label to the option group | |
$elements_select_options['FIELDSLUG']['label'] = 'LABEL'; | |
// Set conditional logic with css classes | |
$elements_select_options['FIELDSLUG']['class'] = 'bf_show_if_f_type_post'; | |
// Now let us add the field slug and label | |
$elements_select_options['FIELDSLUG']['fields']['FIELDSLUG'] = array( | |
'label' => __( 'LABEL', 'buddyforms' ), | |
'unique' => 'unique' // If the form element should only exist one time | |
); | |
return $elements_select_options; | |
} | |
/* | |
* Create the new form builder form element | |
* | |
* Use buddyforms_form_element_add_field filter to add fields to the form element group | |
* $form_fields --> All form element fields | |
* $form_slug --> The form slug | |
* $field_type --> The field type you have defined before in the link | |
* $field_id --> A uique field ID automatically created | |
*/ | |
add_filter('buddyforms_form_element_add_field','my_buddyforms_form_builder_form_element',1,5); | |
function my_buddyforms_form_builder_form_element($form_fields, $form_slug, $field_type, $field_id){ | |
global $post; | |
// Get the form options | |
$buddyform = get_post_meta($post->ID, '_buddyforms_options', true); | |
// Only get in action if the new form element type is processed. | |
// I use a switch statement because you can have many form elements. | |
switch ($field_type) { | |
// Make sure we are on the correct form element type | |
case 'THE-NEW-FIELD-TYPE-SLUG': | |
// Create a variable for your field value | |
$new_option = false; | |
// Check if your field exists and assign the value to your variable | |
if (isset($buddyform['form_fields'][$field_id]['new_option'])) | |
$new_option = $buddyform['form_fields'][$field_id]['new_option']; | |
// Add the form field to the form field array. | |
// The first array defines the section. You can use the default 'general', 'validation', or advanced | |
// You can also create a new section. Just give it a new name and the section will be generated. | |
// The second array is the form element name | |
$form_fields['general']['new_option'] = new Element_Select( | |
__('Field Label', 'buddyforms'), // The field label | |
"buddyforms_options[form_fields][" . $field_id . "][new_option]", // Field option name | |
array( | |
'option-1' => 'Option 1', // The select option array | |
'option-2' => 'Option 2' | |
), | |
array( | |
'value' => $new_option, // The value | |
'class' => '', // Additional class name | |
'id' => $field_id // The field id | |
) | |
); | |
// Add more form elements | |
break; | |
} | |
// Retun the form fields | |
return $form_fields; | |
} | |
/* | |
* Display the new form element in the frontend form | |
* | |
*/ | |
add_filter('buddyforms_create_edit_form_display_element','my_buddyforms_create_frontend_element',1,2); | |
function my_buddyforms_create_frontend_element($form, $form_args){ | |
global $buddyforms; | |
// Extract the form args | |
extract($form_args); | |
// Make sure the form element has a type value | |
if(!isset($customfield['type'])) | |
return $form; | |
// Switch statement to find the form element type we'd like to display | |
switch ($customfield['type']) { | |
case 'THE-NEW-FIELD-TYPE-SLUG': | |
// Add form elements with parameter from $customfield | |
// $form->addElement(...) | |
// The custom field array includes all values from the form buiöder form element | |
// This example if from a taxonomy | |
$form->addElement( new Element_Select( $customfield['name'], $customfield['slug'], The options variable, array('value' => $customfield_val, 'class' => '', 'data-id' => $customfield['slug'], 'data-taxonomy' => $customfield['taxonomy'] ))); | |
// just an example of a html form element | |
$form->addElement( new Element_HTML('')); | |
break; | |
} | |
// Return the form element | |
return $form; | |
} | |
// Save the form element after submit | |
add_action('buddyforms_update_post_meta', 'buddyforms_afe_update_post_meta', 10, 2); | |
function buddyforms_afe_update_post_meta($customfield, $post_id){ | |
// check if it's the correct form elment type | |
if( $customfield['type'] != 'THE-NEW-FIELD-TYPE-SLUG' ) | |
return; | |
// check if the form element exists in the $_POST. You don't need to think about ajax, this gets mapped to the $_POST for you | |
if(!isset($_POST[$customfield['slug']])) | |
return; | |
// Process the form element however you want. Save as post meta, taxonomy or whatever is needed | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment