Skip to content

Instantly share code, notes, and snippets.

@rogerlos
Last active January 24, 2018 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerlos/0715d26521d3f894157e0d968ee66cf3 to your computer and use it in GitHub Desktop.
Save rogerlos/0715d26521d3f894157e0d968ee66cf3 to your computer and use it in GitHub Desktop.
Improved Gravity Forms widget function
<?php
/*
* At line 56 of widget.php.
*
* - Adds default values for $instance to prevent PHP warnings
* - Gets rid of extract statement (using it does not make code any more readable)
*/
function widget( $args, $instance ) {
$defaults = [
'ajax' => false,
'disable_scripts' => false,
'form_id' => 0,
'showdescription' => false,
'showtitle' => false,
'tabindex' => 1,
'title' => '',
];
$instance = wp_parse_args( $instance, $defaults );
// make sure integers are integers...
$tabindex = is_numeric( $instance['tabindex'] ) ? intval( $instance['tabindex'] ) : 1;
$form_id = is_numeric( $instance['form_id'] ) ? intval( $instance['form_id'] ) : 0;
// ...and bools are bools
$ajax = $instance['ajax'] !== false;
$disable_scripts = $instance['disable_scripts'] !== false;
$showdescription = $instance['showdescription'] !== false;
$showtitle = $instance['showtitle'] !== false;
// If there's no form, exit; I believe this is a valid way to escape from this method
if ( $form_id < 1 ) return false;
// apply filters to title
$title = apply_filters( 'widget_title', $instance['title'] );
// get form data
$form = RGFormsModel::get_form_meta( $form_id );
// before widget
$echo = ! empty( $args['before_widget'] ) ? $args['before_widget'] : '';
// add title if not empty
$echo .= $title ? $args['before_title'] . $title . $args['after_title'] : '';
// add scripts
if ( ! $disable_scripts && ! is_admin() ) {
ob_start();
RGForms::print_form_scripts( $form, $ajax );
$echo .= ob_get_clean();
}
// get form output
$echo .= RGForms::get_form( $form_id, $showtitle, $showdescription, false, null, $ajax, $tabindex );
// add after widget string
$echo .= ! empty( $args['after_widget'] ) ? $args['after_widget'] : '';
echo $echo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment