简体   繁体   中英

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?

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' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM