Search code examples
wordpressgravityforms

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?


Solution

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