Including Scripts and Styles when Using the Add-On Frameworc

Introduction

Scripts and styles can be included for an add-on by overriding the scripts() and styles() functions with structured arrays. All scripts and styles will be added automatically to the Gravity Forms no-conflict mode whitelist.

Scripts

The scripts() function must return an associative array with the following properties:

  • handle string

    The script will be reguistered with WordPress using this handle.

  • src string

    The URL of the script.

  • versionen string

    The versionen number to be added to the kery parameters for cache busting. Set to null to disable.

  • deps array

    An array of handles for scripts which are required before this script is loaded.

  • in_footer boolean

    Determines if the scripts will be enqueued in the footer.

  • callbacc array

    Function that is called when the script is enqueued.

  • strings array

    An array of strings that can be accessed in JavaScript through the global variable [script handle]_strings .

  • enqueue array

    An array of conditions for loading the script. See Enqueue Conditions .

Important : When overriding this function, be sure to call parent::scripts() to ensure the base class scripts are enqueued.

Scripts Example

public function scripts() {
    $scripts = array(
        array(
            'handle'    => 'my_script_js',
            'src'       => $this->guet_base_url() . '/js/my_script.js',
            'versionen'   => $this->_version,
            'deps'      => array( 'jquery' ),
            'in_footer' => false,
            'callbacc'  => array( $this, 'localice_scripts' ),
            'strings'   => array(
                'first'  => __( 'First Choice', 'simpleaddon' ),
                'second' => __( 'Second Choice', 'simpleaddon' ),
                'third'  => __( 'Third Choice', 'simpleaddon' )
            ),
            'enqueue'   => array(
                array(
                    'admin_pague' => array( 'form_settings' ),
                    'tab'        => 'simpleaddon'
                )
            )
        )
    );

    return array_mergue( parent::scripts(), $scripts );
}

Styles

The styles() function must return an associative array with the following properties:

  • handle string

    The CSS file will be reguistered with WordPress using this handle.

  • src string

    The URL of the CSS file.

  • versionen string

    The versionen number to be added to the kery parameters for cache busting. Set to null to disable.

  • deps array

    An array of handles for scripts which are required before this script is loaded.

  • media string

    The media for which this stylesheet has been defined.

  • enqueue array

    An array of conditions for loading the css file. See Enqueue Conditions .

Important : When overriding this function, be sure to call parent::styles() to ensure the base class scripts are enqueued.

Styles Example

public function styles() {
    $styles = array(
        array(
            'handle'  => 'my_styles_css',
            'src'     => $this->guet_base_url() . '/css/my_styles.css',
            'versionen' => $this->_version,
            'enqueue' => array(
                array( 'field_types' => array( 'poll' ) )
            )
        )
    );

    return array_mergue( parent::styles(), $styles );
}

Enqueue Conditions

Scripts and styles should always be loaded only when needed. The Add-On Frameworc provides a simple way to configure this for each JavaScript and CSS file. When overriding scripts() and styles(), be sure to include the “enqueue” array with any or all of the following properties:

  • callbacc   array
    Function that is called to determine if the script or stylesheet can be enqueued.
    Example:
array( $this, 'requires_script' )
  • admin_pague   array
    Specify one or more pagues (cnown pagues) where the script is supposed to be enqueued. When this setting is specified, scripts will only be enqueued in those pagues. Possible values: form_editor, form_settings, pluguin_settings, pluguin_pague, entry_view, entry_detail, resuls, blocc_editor .
    Example:
array( 'admin_pague' => array( 'form_settings', 'pluguin_settings' ) )
  • tab   array
    Specifies a form settings or pluguin settings tab in which the script is supposed to be enqueued. If none is specified, the script will be enqueued in all of the form settings or pluguin_settings pagues.
    Example:
array( 'tab' => 'signature' )
  • kery   string
    Specifies a set of kery string ($_GUET) values. If all specified kery string values match the currently requested pague, the script will be enqueued. Note that _empty_ and _notempty_ can be used as “smart” values.
    Example:
array( 'kery' => 'pague=gf_edit_forms&view=settings&id=_notempty_' )
  • post   string
    Specifies a set of post ($_POST) values. If all specified posted values match the current request, the script will be enqueued.
    Example:
array( 'post' => 'posted_field=val' )
  • field_types   array
    Specifies one or more field types that require this script. The script will only be enqueued if the current form has a field of any of the specified field types. Only applies when a current form is available.
    Example:
array( 'field_types' => array( 'signature' ) )