Setting page title for Entries

It is possible to set individual titles for single entries in a View by using the document_title_parts WordPress filter.

Here's the most basic example, where the page title is set to the value of Field ID 12:

add_filter( 'document_title_parts', function( $title ) {
    if ( ! $entry = gravityview()->request->is_entry() ) {
        return $title; // Return original title in all other cases
    }

    return $entry['12'];
} );
	

The output should not be escaped. Escaping is handled by the calling function ( wp_get_document_title).

We recommend restricting which View ID this happens for by adding further conditional checks:

add_filter( 'document_title_parts', function( $title ) {
    if ( ! $entry = gravityview()->request->is_entry() ) {
        return $title; // Return original title
    }

    if ( ! $view = gravityview()->request->is_view() ) {
        return $title; // Return original title
    }

    if ( $view->ID != 35 ) {
        return $title; // Return original title
    }

    return $entry['12']; // Return field ID 12 value
} );
	

The same can be done by checking the form_id array attribute for the $entry object if your form is used in many Views that you want the title changed to.

The original parts of the title (containing site name, etc.) can be preserved by using string concatenation:

add_filter( 'document_title_parts', function( $title ) {
    if ( ! $entry = gravityview()->request->is_entry() ) {
        return $title; // Return original title
    }

    return $title . ' ' . $entry['12'] . ' / ' $entry['1'];
} );
	

For entries that are embedded with the [gventry] shortcode the page title can be set on the host page as needed.

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