Matus Vrsansky
Matus Vrsansky

Reputation: 83

How to disable render HTML code of certain ID of gravity form?

I need to disable render HTML of a certain id in my Gravity Form.

I already found something like this:

add_filter( 'gform_field_content', function ( $field_content, $field, $value ) {
    if ( $field->id == 2 ) {
        if ( $field->is_entry_detail_edit() ) {
            $value = esc_attr( $value );
            $name  = 'input_' . esc_attr( $field->id );

            return "<input type='hidden' name='{$name}' value='{$value}'>";
        } elseif ( $field->is_entry_detail() ) {
            return '';
        }
    }

    return $field_content;
}, 10, 3 );

That one will hide my id, but HTML is stil rendered.

I suppose I need to use filter => gform_pre_render

Someone has some advice for me, please?

Upvotes: 0

Views: 1218

Answers (2)

Mike
Mike

Reputation: 678

Something like this should do it:

function remove_field_by_id($objForm)
{        
    foreach ($objForm['fields'] as $iIndex => $objField) {
        // if its the one you want to remove ...
        if ($objField->id == 3) {
            // replace that field object with an empty array
            array_splice($objForm['fields'], $iIndex, 1, array());
        }
    }
    return $objForm;
}
add_filter('gform_pre_render', 'remove_field_by_id');

Upvotes: 0

Leigh Bicknell
Leigh Bicknell

Reputation: 872

The code you have given prevents the html being output on the entry detail section. Not the main form output.

Try something like this:

add_filter( 'gform_field_content', function ( $field_content, $field, $value ) {
    if ( $field->id == 2 ) {
        // Show the field in entry_detail and form editor
        if ( GFCommon::is_entry_detail_view() || GFCommon::is_form_editor()) {
            return $field_content;
        }

        // Otherwise don't show the field
        return '';
    }

    // Show all other fields
    return $field_content;
}, 10, 3 );

If you want to remove the container list-item tag also try this:

add_filter( 'gform_field_container', function ( $field_container, $field, $form, $css_class, $style, $field_content ) {
    if ( GFCommon::is_entry_detail_view() || GFCommon::is_form_editor()) {
        return $field_container;
    }
    if ( $field->id == 2 ) {
        return '';
    }
    return $field_container;
}, 10, 3);

Upvotes: 2

Related Questions