<?php // Disables form submission when pressing Enter, unless user has tabbed to a form button.
add_action( 'gform_pre_render', function ( $form ) { ?>
<script type="text/javascript">
jQuery(document).bind('gform_post_render', function() {
jQuery(document).on( 'keypress', '.gform_wrapper', function (e) {
var code = e.keyCode || e.which;
if ( code == 13 && ! jQuery( e.target ).is( 'textarea,input[type="submit"],input[type="button"]' ) ) {
e.preventDefault();
return false;
}
});
});
</script>
<?php return $form;
} );
Gravity Forms
Route Notifications Via Username & Email
Format your email field(s) in notifications as {usernames:user1:user2}{emails:account@example.com}
or {usernames:user1:user2:user3}
or as usual.
// Allow Gravity Form notification email fields to use usernames and email
// Format your email fields in notifications as {usernames:user1:user2:user3} or {usernames:user1:user2}{emails:account@example.com}
add_filter( 'gform_notification', function ( $notification, $form, $entry ) {
// run all email fields in the notification through route translation
if ( $notification['toType'] == 'email' ) {
$notification['to'] = typewheel_gform_notification_translate_routes( $notification['to'] );
} else if ( $notification['toType'] == 'routing' ) {
foreach ( $notification['routing'] as $key => $route )
$notification['routing'][ $key ]['email'] = typewheel_gform_notification_translate_routes( $notification['routing'][ $key ]['email'] );
}
$notification['from'] = typewheel_gform_notification_translate_routes( $notification['from'] );
$notification['replyTo'] = typewheel_gform_notification_translate_routes( $notification['replyTo'] );
$notification['bcc'] = typewheel_gform_notification_translate_routes( $notification['bcc'] );
return $notification;
}, 10, 3 );
// reusable function for translating routes
function typewheel_gform_notification_translate_routes( $routes ) {
if ( strpos( $routes, '{usernames' ) !== false || strpos( $routes, '{emails' ) !== false ) {
// parse the username and email batches
$routes = str_replace( array( '} {', '},{' ), '}{', $routes );
$batches = explode( '}{', substr( $routes, 1, -1 ) );
foreach ( $batches as $key => $batch ) {
if ( strpos( $batch, 'usernames' ) !== false )
$usernames = explode( ':', $batch );
if ( strpos( $batch, 'emails' ) !== false )
$emails = explode( ':', $batch );
}
$to = array(); // initialize the to array
// loop through users and grab the email
if ( isset( $usernames ) ) {
unset( $usernames[0] );
foreach ( $usernames as $username ) {
$user = get_user_by( 'login', $username );
if ( $user !== false ) $to[] = $user->user_email;
}
}
// loop through the email addresses
if ( isset( $emails ) ) {
unset( $emails[0] );
foreach ( $emails as $email ) $to[] = $email;
}
// add all addresses to the notification
$routes = implode( ',', $to );
}
return $routes;
} // end typewheel_gform_notification_translate_routes()
Set Number of Days Until Purge of Incomplete Submissions
@replace @int {{days}}
// Overrides the number of days until incomplete submissions (from Save & Continue) are purged
add_filter( 'gform_incomplete_submissions_expiration_days', function ( $expiration_days ) {
return {{days}};
} );
Capture Post Content in a Gravity Form Field
Optionally @replace .entry-content
, <!-- START CONTRACT -->
, <!-- END CONTRACT -->
// Send all HTML content between contentStart and contentEnd comments into Gravity Form paragraph text field with class of `content_receiver`
<script>
jQuery(document).ready(function(){
const contentStart = '<!-- START CONTRACT -->';
const contentEnd = '<!-- END CONTRACT -->';
const contentFull = jQuery('.entry-content').html();
const contentSent = contentFull.substr(0,contentFull.indexOf(contentEnd)).substr(contentFull.indexOf(contentStart)).replace(contentStart,'');
jQuery('.content_receiver textarea').val(contentSent.replace(/\n/gm,' '));
});
</script>
Show Field(s) Across Multiple Pages of Gravity Form
@replace {{_formID}}
<?php // Prepends or appends fields (marked by class `prepend-to-pages` or `append-to-pages` respectively) to every page of a Gravity Form
add_action( 'gform_pre_render{{_formID}}', function( $form ) {
add_action( 'wp_footer', function () { ?>
<script type="text/javascript">
jQuery(document).bind('gform_post_render', function(event, form_id, current_page) {
jQuery(jQuery('.gfield.prepend-to-pages').get().reverse()).each(function() {
jQuery(this).prependTo(`#gform_page_${form_id}_${current_page} .gform_fields`);
});
jQuery(jQuery('.gfield.append-to-pages').get().reverse()).each(function() {
jQuery(this).appendTo(`#gform_page_${form_id}_${current_page} .gform_fields`);
});
} );
</script>
<?php } );
return $form;
} );
Dynamically Populate ACF Select Field With Existing Gravity Forms
Add gravityforms
as field’s Wrapper Attributes class.
// Dynamically populate the options of an ACF select field with Gravity Forms existing on the site
if ( class_exists( 'GFForms' ) ) {
add_filter( 'acf/load_field/type=select', function( $field ) {
if ( 'gravityforms' == $field['wrapper']['class'] ) {
$gf['forms']['active'] = GFAPI::get_forms();
$gf['forms']['inactive'] = GFAPI::get_forms( false );
$gf['forms']['all'] = array_merge( $gf['forms']['active'], $gf['forms']['inactive'] );
foreach ( $gf['forms']['all'] as $form ) {
$field['choices'][ $form['id'] ] = $form['title'];
}
}
return $field;
} );
}
Reposition Gravity Forms Menu Item
@replace @int {{position}}
{{position}} = 4 (before Posts)
{{position}} = 50 (after Comments)
// Set position for the Gravity Forms admin menu item
add_filter( 'gform_menu_position', function( $position ) {
return {{position}};
} );
Limit Countries
@replace {{_formID}}
// Filter the countries that are selectable in an address field
add_filter( 'gform_pre_render{{_formID}}', 'typewheel_limit_countries' );
add_filter( 'gform_pre_validation{{_formID}}', 'typewheel_limit_countries' );
add_filter( 'gform_pre_submission_filter{{_formID}}', 'typewheel_limit_countries' );
add_filter( 'gform_admin_pre_render{{_formID}}', 'typewheel_limit_countries' );
function typewheel_limit_countries( $form ) {
add_filter( 'gform_countries', function( $countries ) {
return array( 'Brazil', 'United States', 'Netherlands', 'United Kingdom' );
});
return $form;
}
Add :filename Modifier to Single File Upload Field
// adds :filename modifier to the single file upload field
add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
if ( $merge_tag != 'all_fields' && $field->type == 'fileupload' && $modifier == 'filename' ) {
$value = str_replace( ' ', '%20', $raw_value );
$pathinfo = pathinfo( $value );
$value = rgar( $pathinfo, 'basename' );
}
return $value;
}, 10, 6 );
Disable All Tooltips
// Disable all administrative tooltips for Gravity Forms
add_filter( 'gform_tooltips', '__return_empty_array' );
Better Style for Gravity Forms Survey Rating Field
/* Better Survey Rating Styles */
:root {
--gsurvey-star-size: 48px;
--gsurvey-star-color: #3b7ca8;
}
body .gform_wrapper.gravity-theme .gsurvey-rating > label {
margin-right: .25em;
}
body .gform_wrapper.gravity-theme .gsurvey-rating > label::before {
display: inline-block;
background-color: #ececec;
border-radius: 50%;
padding: .25em;
}
body .gform_wrapper.gravity-theme .gsurvey-rating > label:hover::before {
color: var(--gsurvey-star-color);
box-shadow: inset 0px 0px 0px 3px var(--gsurvey-star-color);
}
body .gform_wrapper.gravity-theme .gsurvey-rating:not(:checked) > label {
width: calc( var(--gsurvey-star-size) + .5em + 3px );
font-size: var(--gsurvey-star-size) !important;
line-height: var(--gsurvey-star-size);
background-image: none;
background-size: var(--gsurvey-star-size) var(--gsurvey-star-size);
}
body .gform_wrapper.gravity-theme .gsurvey-rating:not(:checked) > label::before {
content: '☆';
}
body .gform_wrapper.gravity-theme .gsurvey-rating > input:checked ~ label {
background-image: none;
background-size: var(--gsurvey-star-size) var(--gsurvey-star-size);
}
body .gform_wrapper.gravity-theme .gsurvey-rating > input:checked ~ label::before {
content: '★';
color: var(--gsurvey-star-color);
}
body .gform_wrapper.gravity-theme .gsurvey-rating:not(:checked) > label:hover,
body .gform_wrapper.gravity-theme .gsurvey-rating:not(:checked) > label:hover ~ label {
background-image: none;
background-size: var(--gsurvey-star-size) var(--gsurvey-star-size);
}
Add CSS Class to Filled Fields
@replace {{class}}
<?php
// Checks whether form inputs are filled and adds a targetable class if they have value
add_action( 'wp_footer', function () { ?>
<script type="text/javascript">
jQuery(document).ready( function() {
function checkFilledInputs() {
jQuery('textarea,input').each( function() { toggleFilledInputs(this); });
jQuery('textarea,input').change( function() { toggleFilledInputs(this); });
}
function toggleFilledInputs(e) {
if ( jQuery(e).val().trim().length > 0 ) {
jQuery(e).addClass('{{class}}');
} else {
jQuery(e).removeClass('{{class}}');
}
}
jQuery(document).bind('gform_post_render', function() { checkFilledInputs(); });
checkFilledInputs();
});
</script>
<?php } );
Add Account Handle Input Mask
// Provide an Account Handle input mask to receive Twitter and Instagram account handles
add_filter( 'gform_input_masks', function( $masks ) {
$masks['Account Handle'] = '@*?**************************';
return $masks;
} );
add_filter( 'gform_input_mask_script', function( $script, $form_id, $field_id, $mask ) {
if ( $mask == '@*?**************************' ) {
$script = "jQuery.mask.definitions['*'] = '[A-Z,a-z,0-9,_]'; jQuery('#input_{$form_id}_{$field_id}').mask('" . esc_js( $mask ) . "',{placeholder:' '}).on('keypress', function(e){if(e.which == 13){jQuery(this).blur();}});";
}
return $script;
}, 10, 4 );
Change Loading Spinner
@replace {{img-src}}
// Set a custom image src for the spinner that runs when processing AJAX requests
add_filter( 'gform_ajax_spinner_url', function( $image_src, $form ) {
return '{{img-src}}';
}, 10, 2 );
Numbered List Rows in Gravity Forms
/* Numbered List Rows in Gravity Forms */
body .gform_wrapper .gform_fields .gfield_list {
counter-reset: gf-list-counter;
}
body .gform_wrapper .gform_fields .gfield_list .gfield_list_header::before {
width: 1.5em;
content: '';
}
body .gform_wrapper .gform_fields .gfield_list .gfield_list_group::before {
width: 1.5em;
line-height: 2.25;
font-weight: bold;
counter-increment: gf-list-counter;
content: counter(gf-list-counter);
}
Order Form List Table By Most Recent
// Order form list table by most recent
add_filter( 'gform_form_list_forms', function( $forms, $search_query, $active, $sort_column, $sort_direction, $trash ) {
if ( ! rgget( 'orderby' ) ) {
usort( $forms, function( $a, $b ) {
return $b->id <=> $a->id;
} );
}
return $forms;
}, 10, 6 );