GravityView Hooks: Customizing the Output of a Field

The gravityview/template/field/output  filter allows you to modify the displayed value of any field in a View before it is rendered on the page. This is useful when you need to transform, reformat, or enhance what your visitors see without changing the underlying entry data.

How It Works

The filter receives two parameters:

Parameter Description
$output The HTML output of the field (string)
$context A GV\Template_Context  object with access to the current field, entry, and View

The $context  object gives you access to:

  • $context->field->ID  - The Gravity Forms field ID
  • $context->view->ID  - The View ID
  • $context->entry  - The current entry object

Example 1: Mapping Values to Readable Labels

If your form stores short codes or abbreviations (e.g., country codes, department codes, status values), you can map them to human-readable labels in the View:

add_filter( 'gravityview/template/field/output', function( $output, $context ) {
    // Only apply to field ID 5
    if ( empty( $context->field->ID ) || $context->field->ID != 5 ) {
        return $output;
    }

    $labels = array(
        'US' => 'United States',
        'GB' => 'United Kingdom',
        'DE' => 'Germany',
        'FR' => 'France',
        'JP' => 'Japan',
    );

    $value = trim( strip_tags( $output ) );

    if ( isset( $labels[ $value ] ) ) {
        return $labels[ $value ];
    }

    return $output;
}, 10, 2 );

Example 2: Adding a Prefix or Suffix

You can append or prepend text to a field's output. For example, adding a unit of measurement to a Number field:

add_filter( 'gravityview/template/field/output', function( $output, $context ) {
    // Only apply to field ID 12 in View ID 45
    if ( empty( $context->field->ID ) || $context->field->ID != 12 ) {
        return $output;
    }

    if ( $context->view->ID != 45 ) {
        return $output;
    }

    $value = trim( strip_tags( $output ) );

    if ( $value !== '' ) {
        return $value . ' kg';
    }

    return $output;
}, 10, 2 );

Example 3: Wrapping Values in Custom HTML

You can wrap specific values in HTML to add visual styling. For example, highlighting a "Priority" field based on its value:

add_filter( 'gravityview/template/field/output', function( $output, $context ) {
    // Only apply to field ID 8
    if ( empty( $context->field->ID ) || $context->field->ID != 8 ) {
        return $output;
    }

    $value = strtolower( trim( strip_tags( $output ) ) );

    $colors = array(
        'high'   => '#d63638',
        'medium' => '#dba617',
        'low'    => '#00a32a',
    );

    if ( isset( $colors[ $value ] ) ) {
        return sprintf(
            '<span style="color: %s; font-weight: bold;">%s</span>',
            esc_attr( $colors[ $value ] ),
            esc_html( ucfirst( $value ) )
        );
    }

    return $output;
}, 10, 2 );

Before Using This Hook

Depending on your use case, you may not need custom code at all:

Read here how to add these code samples to your website: Where to put code samples

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us