• Resolved saugstrup

    (@saugstrup)


    Hi,

    what is the best way to put information in the email from contact form 7 about the page url the form was submitted from?

    We are thinking about using the same contact for for a client site where the same form will be displayed on several different product pages. We need to know from what page the form has been submitted.

    How to do that best?

Viewing 15 replies - 1 through 15 (of 23 total)
  • I have the same issue. But this plugin tries to keep as simple as posible. Maybe you can use another plugin o modify the current one (take in consideration that any update on the plugin will destroy the modification). I did this and work for me:

    Go to wp-content/plugins/contact-form-7/includes/mail.php

    Edit after line 27 (the one that starts with$mail_body =

    Add this code (or similar):

    //Added to get the current URL
    function selfURL() {
    	$s = empty($_SERVER["HTTPS"]) ? ''
    		: ($_SERVER["HTTPS"] == "on") ? "s"
    		: "";
    	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    	$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
    		: (":".$_SERVER["SERVER_PORT"]);
    	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    }
    function strleft($s1, $s2) {
    	return substr($s1, 0, strpos($s1, $s2));
    }
    $pageURL = selfURL();
    
    //Added to send the current URL
    	$mail_body .= "\n\nSent from URL: $pageURL";

    This code will add the URL after the main content of the form. Hope it works for you. And don’t forget that any update in the plugin will make this to desappear.

    Thread Starter saugstrup

    (@saugstrup)

    EXACTLY what we needed! Thanks a bunch, man! ??

    in the new version this isnt possible since i don’t have any mail.php in my includes.

    Would really like to have the current webpage that the form was sent from in the mail.

    Would also like an ajax-script that hides the form when not used. Then when pressed on a button the form will appear under the current post.

    Anyone knows a solution for this?

    The solution for version 2:

    Open includes/classes.php

    Add this before the function compose_and_send_mail (line 315)

    //Added to get the current URL
    function selfURL() {
    	$s = empty($_SERVER["HTTPS"]) ? ''
    		: ($_SERVER["HTTPS"] == "on") ? "s"
    		: "";
    	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    	$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
    		: (":".$_SERVER["SERVER_PORT"]);
    	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    }
    function strleft($s1, $s2) {
    	return substr($s1, 0, strpos($s1, $s2));
    }

    Then add this after $mail_body inside function compose_and_send_mail (line 324)

    //Added to send the current URL
    	$pageURL = selfURL();
    	$mail_body .= "\n\nSent from URL: $pageURL";

    Again, this is just a way to do it, but it’s better using new modules options in Contact Form 7 v2. Because then you won’t lose the code during updating.

    Is there an example of how this can be done using the new modules..?

    Thanks

    I’ve actually been trying to get your method to work, but have no success yet…so any help on the modules would be great…thanks

    Here is a module which does the trick:
    <?php
    /**
    ** module, which adds a computed values to be displayed in the resulting message:
    ** [pagelink name] -> permalink of that post/page is available via [name]
    **/

    /* Shortcode handler */

    function wpcf7_pagelink_shortcode_handler( $tag ) {
    global $wpcf7_contact_form;

    if ( ! is_array( $tag ) )
    return ”;

    $type = $tag[‘type’];
    $name = $tag[‘name’];

    $html = ‘<input type=”hidden” name=”‘. $name .'” value=”‘.get_the_ID().'” />’;

    return $html;
    }

    wpcf7_add_shortcode( ‘pagelink’, ‘wpcf7_pagelink_shortcode_handler’, true );

    function wpcf7_pagelink_validation_filter( $result, $tag ) {
    global $wpcf7_contact_form;

    $type = $tag[‘type’];
    $name = $tag[‘name’];

    $thePageID = (int) $_POST[$name] ;

    if ( $thePageID == 0 ) {
    $result[‘valid’] = false;
    $result[‘reason’][$name] = ‘Someone tampered with the form! Bad guy!’;
    return $result;
    }

    $_POST[$name] = get_permalink($thePageID);

    return $result;
    }

    add_filter( ‘wpcf7_validate_pagelink’, ‘wpcf7_pagelink_validation_filter’, 10, 2 );

    ?>

    Thanks…I’m almost there. I created a module page with your code and named it pagelink.php

    What’s the [shortcode] in the message area to have the page link show up in the email?

    In the form its [pagelink mypagelink] and in the message you can access this pagelink with [mypagelink]. Of course, “mypagelink”-string needs to be the same in both cases, but can be something else as well. It just needs to be there…

    Thanks for this…now I just need to figure a way to embed a form, that actually works, with some kind if invocation code.

    I am also using one form on multiple pages. Is there a simpler way to tell which exact page the form submission came from ?

    The module method by schulzjan is extremely simple…you’ll have it set-up in less than 5.

    This solution is great and works perfectly.

    To newies (like me):

    1. Create an empty file named pagelink.php (or similar) into plugins/contact-form-7/modules/
    2. Copy-paste the php code writen by schulzjan above.
    3. Save the file.
    4. Go to your Contact Form 7 admin page and add the tag [pagelink mypagelink] somewhere inside your form.
    5. Finally, add the tag [mypagelink] somewhere inside the body of the message.

    Thanks schulzjan!!

    Just remember to keep a copy of the pagelink.php (module) on your local hard drive…for some reason when updating the plugin it deletes it, so you will need to re-upload after every update.

    I get the following error when I try to update the form (step 4 above):

    Warning: Cannot modify header information – headers already sent by (output started at /home3/cdnbguco/public_html/wp-content/plugins/contact-form-7/modules/pagelink.php:46) in /home3/cdnbguco/public_html/wp-includes/pluggable.php on line 865

    Any ideas?

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Plugin: Contact Form 7] Source URL for Contact form 7?’ is closed to new replies.