Site icon Nabtron

Gravity forms tricks

Gravity forms is an amazing plugin itself, however there are always some situations when we don’t have the built-in functionality for what we need.

Here we will discuss how to hack gravity forms plugin for WordPress as per our own needs in various situations. This article will keep updating as we get more hacks into it.

Include Widget inside Gravity forms

Versions

WordPress: 4.4.2

Gravity forms: 1.9.17.6

Currency converter: 2.1

Aim:

For this project, we needed to show a currency converter right inside the forms, where the user will input the currency in input field.

For currency conversion, we’re using plugin:

Summary:

We’ll be creating a new widget position.

Then we’ll drop the currency conversion widget in that widget position.

We will call that custom widget position inside a gravity forms hook to show that data.

We’ll use PHP output buffering, because calling widget directly outputs (via echo) the output of the widget code, which we don’t want.

Code used:

function custom_currency_converter_widgets_init() {
  register_sidebar( array(
    'name' => 'Currency converter in form',
    'id' => 'custom_currency_converter_1',
    'before_widget' => '<div>',
    'after_widget' => '</div>',
    'before_title' => '<h2 class="rounded">',
    'after_title' => '</h2>',
  ) );
}
add_action( 'widgets_init', 'custom_currency_converter_widgets_init' );

add_filter( 'gform_field_input', 'custom_currency_converter', 10, 5 );
function custom_currency_converter( $input, $field, $value, $lead_id, $form_id ) {
  // because this will fire for every form/field, only do it when it is the specific form and field
  if ( $form_id == 1 && $field->id == 13 ) {
    if ( is_active_sidebar( 'custom_currency_converter_1' ) ) {
      ob_start();
      dynamic_sidebar( 'custom_currency_converter_1' );
      $input = ob_get_contents();
      ob_end_clean();
    }
  }
  return $input;
}

Let me know if there is any confusion or query.

Exit mobile version