Skip to content

Instantly share code, notes, and snippets.

View richardW8k's full-sized avatar

Richard Wawrzyniak richardW8k

  • Rocketgenius, Inc (@gravityforms)
  • Lancashire, UK
  • 18:43 (UTC +01:00)
View GitHub Profile
@richardW8k
richardW8k / button_filters.php
Last active April 29, 2022 18:37
convert the next, previous and/or submit button <input> to <button> and uses the input value to create the button text inside a <span>, this means you can set the button text by editing the form or page break settings.
<?php
/**
* Filters the next, previous and submit buttons.
* Replaces the forms <input> buttons with <button> while maintaining attributes from original <input>.
* @param string $button Contains the <input> tag to be filtered.
* @param object $form Contains all the properties of the current form.
* @return string The filtered button.
*/
add_filter( 'gform_next_button', 'input_to_button', 10, 2 );
add_filter( 'gform_previous_button', 'input_to_button', 10, 2 );
function listFieldDatepicker(formId, fieldId, column) {
var listField = '#field_' + formId + '_' + fieldId,
columnClass = '.gfield_list_' + fieldId + '_cell' + column + ' input';
jQuery.datepicker.setDefaults({
showOn: 'both',
buttonImage: '/wp-content/plugins/gravityforms/images/calendar.png',
buttonImageOnly: true,
dateFormat: 'dd/mm/yy',
firstDay: 1
});
@richardW8k
richardW8k / calculatedShipping
Last active January 1, 2016 07:49
Script to set shipping price as the result of a calculated number field
<script>
gform.addFilter( 'gform_calculation_result', function(result, formulaField, formId, calcObj ){
if ( formulaField.field_id == "4" ){
jQuery(".ginput_shipping_price").html(gformFormatMoney(result)).prev('input').val(gformFormatMoney(result));
gformCalculateTotalPrice( formId );
}
return result;
});
</script>
@richardW8k
richardW8k / radioProductOther
Last active January 1, 2016 01:29
set's value of radio product field other choice to value entered in fake text field
<input id="choice_1_3_other" type="text" value="" class="medium ginput_amount">
<script>
jQuery('#choice_1_3_other').change(function() {
var $other = jQuery('#choice_1_3');
var val = $other.val().split("|");
$other.val( val[0] + '|' + gformToNumber(jQuery(this).val()) );
gformCalculateTotalPrice(formId);
});
</script>
@richardW8k
richardW8k / log_validation_failure
Last active December 31, 2015 08:38
Log Gravity Forms validation errors by adding the following code to your theme's functions.php file
// log field validation errors
add_filter( 'gform_validation', 'log_validation_failure', 30 );
function log_validation_failure( $validation_result ) {
$form = $validation_result['form'];
foreach ( $form['fields'] as &$field ) {
if ( $field['failed_validation'] )
GFCommon::log_error( "form #{$form["id"]}: validate() - failed: {$field["label"]}({$field["id"]} - {$field["type"]}) - message: {$field["validation_message"]}" );
}
return $validation_result;
}
@richardW8k
richardW8k / ListFieldMaskedInput
Last active December 28, 2015 16:29
Activate input mask for list field columns. If no other field types have input masks enabled you will also need to enqueue the masked input script, see the following gist for an example https://gist.github.com/richardW8k/7529522
<script>
function ListFieldMaskedInput( formId, fieldId, column, mask ){
jQuery( '.gfield_list_' + fieldId + '_cell' + column + ' input' ).mask( mask );
jQuery( '#field_' + formId + '_' + fieldId ).on( 'click', '.add_list_item', function(){
jQuery( '.gfield_list_' + fieldId + '_cell' + column + ' input' ).mask( mask )
});
}
ListFieldMaskedInput( 11, 2, 1, '9999' );
/**
* Add a function call for each list field column you want to activate an input mask for
@richardW8k
richardW8k / ListFieldMaskedInput
Last active December 28, 2015 16:29
Activate input masks for List Field columns.
add_filter('gform_enqueue_scripts_11', 'list_columns_mask_script');
function list_columns_mask_script() {
wp_enqueue_script( 'gform_masked_input', array( 'jquery' ), false, true );
}
add_filter('gform_register_init_scripts_11', 'list_columns_mask_init');
function list_columns_mask_init( $form ) {
$init = "";
/**
* Add a function call for each list field column you want to activate an input mask for
* $init .= "ListFieldMaskedInput( formId, fieldId, column, 'mask' );";
@richardW8k
richardW8k / log_uploads
Last active July 14, 2021 17:10
Logs file uploads for single file and multi-file upload fields pre and post submission to the Gravity Forms logging add-on: http://www.gravityhelp.com/downloads/#Gravity_Forms_Logging_Tool
add_action( 'gform_pre_submission_8', 'uploads_log_pre_submission' );
function uploads_log_pre_submission( $form ) {
foreach ( $form["fields"] as &$field ) {
if ( $field["type"] == "fileupload" ) {
$id = "input_".$field["id"];
if ( $field["multipleFiles"] ) {
if ( empty( $_POST["gform_uploaded_files"] ) )
continue;
@richardW8k
richardW8k / ListFieldMaskedInput
Created November 14, 2013 16:54
add an input mask to an input in a Gravity Forms list field column
// enable input mask for form #95 list field #3 columns 1 and 3
add_filter('gform_register_init_scripts_95', 'enable_list_input_mask');
function enable_list_input_mask($form) {
$field_id = "3"; //set field id here
$col_id = array('1','3'); //set column id here
$mask = "9999"; //define mask here, examples at http://www.gravityhelp.com/documentation/page/Input_Mask
//that's it nothing more to configure
$c_sel = array();
foreach($col_id as $c) {
@richardW8k
richardW8k / form_settings_status
Last active December 27, 2015 19:09
adds form active/inactive status switch to Gravity Forms form settings page
// display form active/inactive status on the form settings view
add_filter('gform_form_settings', 'form_settings_status', 10, 2);
function form_settings_status($settings, $form){
$form_info = RGFormsModel::get_form($form["id"]);
$status_val = intval($form_info->is_active);
$status = $status_val ? __('Active', 'gravityforms') : __('Inactive', 'gravityforms');
$settings["Form Basics"]["form_status"] = "<tr><th><label>Form status</label></th><td><img class='gform_active_icon' src='".GFCommon::get_base_url()."/images/active{$status_val}.png' style='cursor:pointer;margin:5px 0 0 0;float:left;' alt='{$status}' title='{$status}' onclick='ToggleActive( this, {$form["id"]} );' /> <span class='gform_status' style='margin:3px 0 0 10px;float:left;' >{$status}</span></td></tr>";
return $settings;
}
// add script to settings page to allow toggling of active/inactive status