Skip to content

Instantly share code, notes, and snippets.

@mattiasghodsian
Last active April 28, 2023 08:33
Show Gist options
  • Save mattiasghodsian/a6ecf354bdb09dd0df8b274606127325 to your computer and use it in GitHub Desktop.
Save mattiasghodsian/a6ecf354bdb09dd0df8b274606127325 to your computer and use it in GitHub Desktop.
[WordPress] Get field data by field ID gravity forms
/**
* gf_get_field_data
* Author: Mattias Ghodsian
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian
* Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5
*
* @param int $form_id
* @param int $id the field integer key
* @param str $key what value to return
* @return string/boolean
*/
function gf_get_field_data( int $form_id, int $id, $val){
global $wpdb;
if ( !is_string($val) )
return "$val must be a string";
$t = $wpdb->prefix.'gf_form_meta';
$q = $wpdb->get_var( "SELECT display_meta FROM {$t} WHERE form_id = {$form_id}" );
if ( !$q )
return false;
$q = json_decode($q);
foreach ($q->fields as $key => $d) {
if ($d->id == $id) {
return $d->{$val};
}
}
}
@mattiasghodsian
Copy link
Author

mattiasghodsian commented Jun 22, 2020

Example
This returns the label Namn of field id 3 of form id 1

gf_get_field_data(1, 3, "label");

What you can expect to return

^ {#1954 ▼
  +"type": "text"
  +"id": 3
  +"label": "Namn"
  +"adminLabel": ""
  +"isRequired": true
  +"size": "medium"
  +"errorMessage": ""
  +"visibility": "visible"
  +"inputs": null
  +"formId": 1
  +"description": ""
  +"allowsPrepopulate": false
  +"inputMask": false
  +"inputMaskValue": ""
  +"inputMaskIsCustom": false
  +"maxLength": ""
  +"inputType": ""
  +"labelPlacement": ""
  +"descriptionPlacement": ""
  +"subLabelPlacement": ""
  +"placeholder": "Namn *"
  +"cssClass": ""
  +"inputName": "frfg_name"
  +"noDuplicates": false
  +"defaultValue": ""
  +"choices": ""
  +"conditionalLogic": ""
  +"productField": ""
  +"enablePasswordInput": ""
  +"multipleFiles": false
  +"maxFiles": ""
  +"calculationFormula": ""
  +"calculationRounding": ""
  +"enableCalculation": ""
  +"disableQuantity": false
  +"displayAllCategories": false
  +"useRichTextEditor": false
  +"fields": ""
  +"displayOnly": ""
  +"pageNumber": 1
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment