SNIPPET · GRATUITO

WordPressGravity Forms

Aggiungi la pagina di provenienza alle email Gravity Forms

Mostra nelle notifiche Gravity Forms la pagina da cui è stato inviato il modulo.

A cosa serve

Se lo stesso form compare su più pagine, aggiunge alla notifica l’URL della pagina da cui è stato inviato. Usa prima source_url salvato da Gravity Forms e, se non disponibile, prova il referrer HTTP.

Requisito: Gravity Forms. La notifica deve contenere il merge tag {all_fields}.

Come usarlo

  1. Installa Code Snippets.
  2. Crea uno snippet PHP.
  3. Incolla il codice e attivalo.
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 );

Come usarlo

  1. Installa Code Snippets Free su WordPress.
  2. Crea un nuovo snippet PHP.
  3. Copia e incolla il codice.
  4. Attivalo nell’ambito indicato.

È un metodo semplice, non obbligatorio: puoi integrare il codice nel tuo plugin o nel punto appropriato. Per gli esempi Gravity Forms configura gli ID e gli eventuali provider prima di attivare.