Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1475507

    HI,
    I would need to add the URL of origin in all forms, so I can know on which page or article the form was filled out.

    How can I do it?
    Thanks for the support!

    #1475519

    Hey daimonart,
    Add a custom page title to each contact form “E-Mail Subject”
    Screen Shot 2025 01 21 at 5.17.05 PM

    Best regards,
    Mike

    #1475560

    Thank you Mike,
    but I have the same form in all the articles, added with a shortcode, which recalls a page with a color section and a form inside.
    I tried putting some code in the object, but it doesn’t work. Where can I find the files relating to the form, so I can study them. Or does anyone know how to do it?
    Thank you so much!

    #1475601

    Hi,
    You could review the file at config-templatebuilder/avia-shortcodes/contact/contact.php
    but changing this file will make all of the forms have the same result, you are best to use a different form on each page with a unique “E-Mail Subject” as posted above.

    Best regards,
    Mike

    #1475608

    maybe it is possible via that filter: avia_contact_form_args ?

    #1475642

    if a hint on the bottom of your e-mail is enough – you can put this to your child-theme functions.php:

    function add_page_info_to_form_email($message, $new_post, $form_params) {
        // Get the current page URL and title
        $page_url = get_permalink();
        $page_title = get_the_title();
        // Append the page information to the message
        $message .= "\n\n--\nSent from the page:\n" . $page_url . "\n";
        return $message;
    }
    add_filter('avf_form_message', 'add_page_info_to_form_email', 10, 3);
    #1475646

    Since I use contactform 7, I can’t say whether this has always been the case: the subject in the email header is always :
    sent by contact form at bloginfo.

    #1475658

    Thank you so much everyone!
    I solved it thanks to your instructions.
    Below is the code used, if it helps, which inserts URL and page title to all forms.

    Thank you for your help and your time!

    ————

    function add_page_info_to_form_email($message, $new_post, $form_params) {
    // Debug: Controlliamo se la funzione viene eseguita
    error_log(‘DEBUG: Entrato in add_page_info_to_form_email’);

    // Recupera il titolo e l’URL della pagina corrente
    $page_url = get_permalink();
    $page_title = get_the_title();

    // Se il titolo è vuoto, proviamo a recuperarlo dall’URL di riferimento
    if (empty($page_title) && isset($_SERVER[‘HTTP_REFERER’])) {
    $referrer_url = esc_url_raw($_SERVER[‘HTTP_REFERER’]);
    error_log(‘DEBUG: Il titolo della pagina è vuoto, provo a recuperarlo dall’URL -> ‘ . $referrer_url);

    $response = wp_remote_get($referrer_url);
    if (!is_wp_error($response)) {
    $body = wp_remote_retrieve_body($response);
    preg_match(‘/<title>(.*?)<\/title>/s’, $body, $matches);
    if (!empty($matches[1])) {
    $page_title = html_entity_decode(trim($matches[1]), ENT_QUOTES | ENT_HTML5, ‘UTF-8’);
    $page_title = preg_replace(‘/[^\x20-\x7E]/’, ”, stripslashes($page_title));
    error_log(‘DEBUG: Titolo recuperato dal corpo della pagina -> ‘ . $page_title);
    }
    }
    }

    // Se il titolo è ancora vuoto, assegna un valore predefinito per il debug
    if (empty($page_title)) {
    $page_title = ‘Titolo non disponibile’;
    error_log(‘DEBUG: Nessun titolo trovato, imposto “Titolo non disponibile”‘);
    }

    // Se l’URL è vuoto, recuperiamolo dall’HTTP_REFERER
    if (empty($page_url) && isset($_SERVER[‘HTTP_REFERER’])) {
    $page_url = esc_url_raw($_SERVER[‘HTTP_REFERER’]);
    error_log(‘DEBUG: URL recuperato da HTTP_REFERER -> ‘ . $page_url);
    }

    // Debug finale prima di aggiungere le informazioni alla mail
    error_log(‘DEBUG: Titolo finale prima di inviare la mail -> ‘ . $page_title);
    error_log(‘DEBUG: URL della pagina -> ‘ . $page_url);

    // Aggiungiamo il titolo e l’URL al fondo del messaggio email
    $message .= “\n\n—\nTitolo della pagina: ” . $page_title . “\nURL: ” . $page_url . “\n”;

    return $message;
    }
    add_filter(‘avf_form_message’, ‘add_page_info_to_form_email’, 10, 3);

    #1475659

    Hi,

    Great, I’m glad that you found a solution and thanks for sharing it. Please let us know if you should need any further help on the topic, or if we can close it.

    Best regards,
    Rikard

    #1475669

    You can close the ticket, thanks!

    #1475671

    Why not using the code tag for your code? Link
    No one can copy paste the code for his own use.

    #1475680

    Hi,

    Thanks for the update, we’ll close this thread for now then. Please open a new thread if you should have any further questions or problems.

    Best regards,
    Rikard

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘Add the URL of the current page to the form’ is closed to new replies.