Record the Page and Referrer in Gravity Forms
Fills hidden Gravity Forms fields with the title and address of the page the form is on and the site the visitor came from, so you know the context of each entry.
Opens WPIDE on
Code
add_filter( 'gform_field_value_page_title', function ( $value ) {
return '' !== (string) $value ? $value : wp_strip_all_tags( (string) get_the_title( get_queried_object_id() ) );
} );
add_filter( 'gform_field_value_page_url', function ( $value ) {
$id = get_queried_object_id();
return '' !== (string) $value ? $value : ( $id ? (string) get_permalink( $id ) : home_url( add_query_arg( array() ) ) );
} );
add_filter( 'gform_field_value_referrer_url', function ( $value ) {
if ( '' !== (string) $value || empty( $_SERVER['HTTP_REFERER'] ) ) {
return $value;
}
return esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
} );
Notes
Allow dynamic population on hidden fields with the parameter names page_title, page_url or referrer_url.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.