Skip to content

Instantly share code, notes, and snippets.

@travislopes
Last active February 24, 2017 21:45
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 travislopes/505788ff411dadafc53404a3369c1808 to your computer and use it in GitHub Desktop.
Save travislopes/505788ff411dadafc53404a3369c1808 to your computer and use it in GitHub Desktop.
This code snippet will automatically open the Gravity Forms field settings pane and navigate to the desired tab on page load.
/***
* This code snippet will automatically open the field settings
* pane and navigate to the desired tab on page load.
*
* To use, just add the tab to the hash of the page URL:
* http://example.com/wp-admin/admin.php?page=gf_edit_forms&id=1#tab=gform_tab_1
*
* By default, the first field in the form will be opened.
* To open a different field, add a field ID parameter:
* http://example.com/wp-admin/admin.php?page=gf_edit_forms&id=1#tab=gform_tab_1&field=4
*/
( function( $ ) {
$( document ).on( 'ready', function() {
setTimeout( openFieldSettings, 100 );
} );
$( window ).on( 'hashchange', openFieldSettings );
function openFieldSettings() {
// If this is not the form editor page, exit.
if ( $( '.gforms_edit_form' ).length == 0 ) {
return;
}
// Get hash.
var hash = window.location.hash.slice( 1 );
hash = hash.split( '&' );
// Initialize paramters object.
var params = {};
// Loop through hash array.
for ( var i = 0; i < hash.length; i++ ) {
// Get key and value.
var values = hash[ i ].split( '=' );
// If key or value is empty, skip.
if ( values[0].length === 0 || values[1].length === 0 ) {
continue;
}
$.map( values, function( val, i ) {
val = val.replace( /[^a-z0-9áéíóúñü \.,_-]/gim, '' );
return val.trim();
} );
// Add to parameters object.
params[ values[0] ] = values[1];
}
// Set default tab.
if ( ! params.tab ) {
return;
}
// Sanitize field ID.
if ( params.field ) {
params.field = parseInt( params.field );
params.field = isNaN( params.field ) ? null : params.field;
}
// Prepare field selector.
var fieldSelector = params.field ? '#field_' + params.field : '.gfield:first-of-type';
// Open field.
if ( ! $( '#gform_fields ' + fieldSelector ).hasClass( 'field_selected' ) ) {
$( '#gform_fields ' + fieldSelector ).trigger( 'click' );
}
// Switch to tab.
$( '#field_settings .ui-tabs-nav li[aria-controls="' + params.tab + '"] a' ).trigger( 'click' );
}
} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment