SNIPPET · FREE

WordPressGravity Forms

Add the source page to Gravity Forms emails

Show the page a Gravity Forms submission came from inside its notification email.

What it does

If the same form appears on multiple pages, this adds the submission source URL to the notification. It prefers Gravity Forms’ saved source_url and falls back to the HTTP referrer.

Requires: Gravity Forms. The notification must contain the {all_fields} merge tag.

How to use it

  1. Install Code Snippets.
  2. Create a PHP snippet.
  3. Paste the code and activate it.
PHP · Gravity Forms · Code Snippets
/**
 * Add the form source page after {all_fields} in Gravity Forms notifications.
 *
 * Uses the entry source_url when available and falls back to the HTTP referrer.
 */

add_filter( 'gform_notification', function ( $notification, $form, $entry ) {

    if ( empty( $notification['message'] ) || strpos( $notification['message'], '{all_fields}' ) === false ) {
        return $notification;
    }

    $source_url = '';

    if ( is_array( $entry ) && ! empty( $entry['source_url'] ) ) {
        $source_url = esc_url_raw( $entry['source_url'] );
    }

    if ( ! $source_url && ! empty( $_SERVER['HTTP_REFERER'] ) ) {
        $source_url = esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
    }

    if ( $source_url ) {
        $source_html = sprintf(
            '<hr><p><strong>%s</strong> <a href="%s">%s</a></p>',
            esc_html__( 'Source page:', 'mrc-snippets' ),
            esc_url( $source_url ),
            esc_html( $source_url )
        );
    } else {
        $source_html = '<hr><p><strong>' .
            esc_html__( 'Source page:', 'mrc-snippets' ) .
            '</strong> ' .
            esc_html__( 'Unavailable', 'mrc-snippets' ) .
            '</p>';
    }

    $notification['message'] = str_replace(
        '{all_fields}',
        '{all_fields}' . $source_html,
        $notification['message']
    );

    return $notification;

}, 10, 3 );

How to use it

  1. Install Code Snippets Free in WordPress.
  2. Create a new PHP snippet.
  3. Copy and paste the code.
  4. Activate it in the required scope.

This is a simple option, not a requirement: experienced users can put the code in their own plugin or the appropriate location. Configure form IDs and any required providers before enabling Gravity Forms examples.