Add a new setting to each form's Settings > General tab or adding a new tab of settings.
Relevant hooks
- add_action('frm_additional_form_options', 'frm_add_new_form_opt' );
- add_filter('frm_form_options_before_update', 'frm_update_my_form_option', 20, 2);
- add_filter('frm_add_form_settings_section', 'frm_add_new_settings_tab', 10, 2);
Add a new setting
Add a new option on the form's Settings > General tab. The new option will appear in a "Miscellaneous" section at the bottom of the General Settings. Please note that $values['id'] is the form ID.
add_action('frm_additional_form_options', 'frm_add_new_form_opt', 10, 1 ); function frm_add_new_form_opt( $values ) { ?> <tr><td colspan="2"> <?php $opt = (array)get_option('frm_myoptname'); ?> <label for="frm_myoptname"><input type="checkbox" value="1" id="frm_myoptname" name="frm_myoptname" <?php echo (in_array($values['id'], $opt)) ? 'checked="checked"' : ''; ?> />This is the label for my option.</label> </td></tr> <?php }
Note that the frm_myoptname value is stored in the wp_options table in the database and it stores an array of form IDs.
Save the setting
Save the setting in the database whenever the form settings are updated. Please note that $values['id'] is the form ID.
add_filter('frm_form_options_before_update', 'frm_update_my_form_option', 20, 2); function frm_update_my_form_option( $options, $values ){ $opt = (array)get_option('frm_myoptname'); if ( isset( $values['frm_myoptname'] ) && ( ! isset($values['id'] ) || !in_array( $values['id'], $opt ) ) ) { $opt[] = $values['id']; update_option('frm_myoptname', $opt); } else if ( ! isset( $values['frm_myoptname'] ) && isset( $values['id'] ) && in_array( $values['id'], $opt ) ) { $pos = array_search( $values['id'], $opt ); unset( $opt[$pos] ); update_option('frm_myoptname', $opt); } return $options; }
Retrieve the setting
You may use any number of hooks to add the desired functionality to a form. You will typically need to retrieve the saved option in order to determine whether or not the functionality should be added. Since the option is added to the wp_options table with the WP update_option, you can retrieve the setting with the WP get_option:
get_option('frm_myoptname')
Replace frm_myoptname with the name of the option. If the option is specific to a form, you may retrieve it like this:
get_option('frm_mysettings_' . $form_id)
Replace frm_mysettings with the name of the option.
Add a settings tab
The examples below save an array of settings to the wp_options table. The key is frm_mysettings_formID, and this can be modified to anything else.
Add a new menu item
add_filter('frm_add_form_settings_section', 'frm_add_new_settings_tab', 10, 2); function frm_add_new_settings_tab( $sections, $values ) { $sections[] = array( 'name' => 'New Tab Name', 'anchor' => 'new_tab_name', 'function' => 'get_my_new_settings', /*'class' => 'YourClassNamewHere'*/ ); return $sections; }
Replace New Tab Name with the name of your new tab. Replace new_tab_name with an all lowercase, no spaces version of your tab name. If your function is in a class, uncomment 'class' => 'YourClassNameHere' and replace YourClassNameHere with the name of the class.
Add the settings
The name of this function should match the name of the function declared in the $sections array above. Please note that $values['id'] is the form ID.
function get_my_new_settings( $values ) { $form_fields = FrmField::getAll('fi.form_id='. (int) $values['id'] ." and fi.type not in ('break', 'divider', 'html', 'captcha', 'form')", 'field_order'); $my_form_opts = maybe_unserialize( get_option('frm_mysettings_' . $values['id']) ); ?> <h3 class="frm_first_h3"><?php _e( 'My Settings', 'formidable' ); ?> <span class="frm_help frm_icon_font frm_tooltip_icon" title="This is my tooltip text."</span> </h3> <table class="form-table"> <tr> <td width="100px"> <label>First Option</label> </td> <td> <select name="frm_mysettings[firstopt]"> <option value=""><?php _e( '— Select —' ); ?></option> <?php foreach ( $form_fields as $form_field ) { $selected = ( isset( $my_form_opts['firstopt'] ) && $my_form_opts['firstopt'] == $form_field->id ) ? ' selected="selected"' : ''; ?> <option value="<?php echo $form_field->id ?>" <?php echo $selected ?>><?php echo FrmAppHelper::truncate( $form_field->name, 40 ) ?></option> <?php } ?> </select> </td> </tr> </table> <?php }
Save the settings
Please note that $values['id'] is the form ID.
add_filter('frm_form_options_before_update', 'frm_save_my_new_settings', 20, 2); function frm_save_my_new_settings( $options, $values ){ if ( isset( $values['frm_mysettings'] ) ) { $new_values = maybe_serialize( $values['frm_mysettings'] ); update_option( 'frm_mysettings_' . $values['id'], $new_values ); } return $options; }