Sort by: Recently Added Likes

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()

#

Prevent Submission When Pressing Enter

<?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;

} );

#

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>

Source

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;

} );

Source

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;

   } );

}

#

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);
}

#

Everyone has their thing. Ours is domain names.

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 );

#

Increase width of form editor sidebar

// Increase width of form editor sidebar
add_action( 'admin_print_styles', function() {

    if ( class_exists( 'GFCommon' ) && GFCommon::is_form_editor() ) {

        echo '<style>
                .gforms_edit_form .form_editor_fields_container { max-width: calc( 100% - 744px ); }
                .gforms_edit_form .form_editor_fields_container.droppable { padding-right: 712px; }
                .gforms_edit_form .editor-sidebar .sidebar,
                .gforms_edit_form .editor-sidebar .sidebar .sidebar__nav { width: 520px; }
                .gforms_edit_form .editor-sidebar .sidebar .add-buttons li { width: 25%; }
              </style>';

    }

} );

#

Set parent for post created with APC to embed post

@replace {{form_id}}

// Set parent for post created with APC to post on which form is embedded
add_action( 'gform_advancedpostcreation_post_after_creation_{{form_id}}',

    function( $post_id, $feed, $entry, $form ) {

        wp_update_post( array(
            'ID' => $post_id,
            'post_parent' => GFCommon::replace_variables( '{embed_post:ID}', $form, $entry )
        ) );

    },

10, 4 );

#

Remove inactive forms from form switcher

// Remove inactive forms from form switcher
add_filter( 'gform_form_switcher_forms', function( $forms ) {

    $forms = array_filter( $forms, function( $form ) {
        return rgobj( $form, 'is_active' );
    } );

    return $forms;

} );

#

Notification Interception

@replace {{_formID}}, {{email}}

// Sends all Gravity Form notifications to the specified email address
add_filter( 'gform_notification{{_formID}}', function ( $notification, $form, $entry ) {

   $notification['toType'] = 'email';
   $notification['to'] = '{{email}}';

   return $notification;

}, 10, 3 );

#

Replace Numbered Months with Named Months in Date Field

<?php // Replace the numbered months with named months in a Gravity Forms dropdown date field
add_action( 'wp_footer', function () { ?>

   <script type="text/javascript">
      jQuery(document).ready(function( $ ) {
         $('.gfield_date_dropdown_month select option').each( function(i, option) {
            switch( option.value ) {
               case '1': option.innerHTML = 'January'; break;
               case '2': option.innerHTML = 'February'; break;
               case '3': option.innerHTML = 'March'; break;
               case '4': option.innerHTML = 'April'; break;
               case '5': option.innerHTML = 'May'; break;
               case '6': option.innerHTML = 'June'; break;
               case '7': option.innerHTML = 'July'; break;
               case '8': option.innerHTML = 'August'; break;
               case '9': option.innerHTML = 'September'; break;
               case '10': option.innerHTML = 'October'; break;
               case '11': option.innerHTML = 'November'; break;
               case '12': option.innerHTML = 'December'; break;
            }
         });
      });
   </script>

<?php } );

#

Custom Merge Tags

<?php // Introduce additional merge tags for use throughout Gravity Forms
add_filter( 'gform_replace_merge_tags', function ( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {

   $custom_merge_tags = array(
      '{date_ymd}'  => date( 'Y.m-M.d', strtotime( $entry['date_created'] ) ),
      '{timestamp}' => time(),
      '{site_url}'  => get_site_url(),
      '{site_name}' => get_bloginfo( 'name' )
   );
    
   return str_replace( array_keys( $custom_merge_tags ), array_values( $custom_merge_tags ), $text);

}, 10, 7 );

// Add our custom merge tags to the dropdown
add_action( 'admin_print_scripts', function () {

   if ( method_exists( 'GFForms', 'is_gravity_page' ) && GFForms::is_gravity_page() ) { ?>

      <script type="text/javascript">
         gform.addFilter('gform_merge_tags', function (mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option) {
             mergeTags["custom"].tags.push({ tag: '{date_ymd}', label: 'Entry Date (Y.m-M.d)' });
             mergeTags["custom"].tags.push({ tag: '{timestamp}', label: 'Current Time (UNIX Timestamp)' });
             mergeTags["custom"].tags.push({ tag: '{site_url}', label: 'Site URL' });
             mergeTags["custom"].tags.push({ tag: '{site_name}', label: 'Site Name' });
             return mergeTags;
          } );
      </script>

   <?php }

 } );

#

The smarter way to manage your WordPress sites.

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}};
} );

Source