Chris
Chris

Reputation: 514

Wordpress Gravity forms get field id by label name

I'm trying to get a field ID based on a field label name, versus hard coding the values.

I can retrieve the form ID given the form name

RGFormsModel::get_form_id($post_title);

Then use the retrieve all fields and much more based on the ID.

RGFormsModel::get_form_meta($id);

However, parsing through the returned values is very clunky.

Reading through the api, it doesn't appear to be a straightforward way to retrieve the field ID based on its label name.

Is there something in the API or a solution that I'm missing?

Upvotes: 2

Views: 5935

Answers (1)

Dave from Gravity Wiz
Dave from Gravity Wiz

Reputation: 2859

Nope, you aren't missing anything. If you want to get a field object by it's label, you'll have to loop through the form object fields property and compare the desired label with that of each field until you find it.

function gw_get_field_by_label( $label ) {
    foreach( $form['fields'] as $field ) {
        if( $field->label == 'My Field Label' ) {
            return $field;
        }
    }
    return false;
}

You could use it like so:

$field = gw_get_field_by_label( 'My Field Label' );

Upvotes: 5

Related Questions