Forums

[resolved] [Plugin: Contact Form 7] Source URL for Contact form 7? (24 posts)

  1. saugstrup
    Member
    Posted 2 years ago #

    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?

  2. pablodgavilan
    Member
    Posted 2 years ago #

    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.

  3. saugstrup
    Member
    Posted 2 years ago #

    EXACTLY what we needed! Thanks a bunch, man! :-)

  4. spajk
    Member
    Posted 2 years ago #

    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?

  5. pablodgavilan
    Member
    Posted 2 years ago #

    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.

  6. semrocc
    Member
    Posted 2 years ago #

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

    Thanks

  7. semrocc
    Member
    Posted 2 years ago #

    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

  8. schulzjan
    Member
    Posted 2 years ago #

    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 );

    ?>

  9. semrocc
    Member
    Posted 2 years ago #

    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?

  10. schulzjan
    Member
    Posted 2 years ago #

    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...

  11. semrocc
    Member
    Posted 2 years ago #

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

  12. nims
    Member
    Posted 2 years ago #

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

  13. semrocc
    Member
    Posted 2 years ago #

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

  14. pablodgavilan
    Member
    Posted 2 years ago #

    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!!

  15. semrocc
    Member
    Posted 2 years ago #

    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.

  16. thebookmistress
    Member
    Posted 2 years ago #

    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?

  17. scott.hack
    Member
    Posted 2 years ago #

    I got the same error as thebookmistress.

    However, I deleted everything in the file again. Then made the tags as suggested and then reinserted all of the code and it worked.

  18. scott.hack
    Member
    Posted 2 years ago #

    BTW, did some googling and found out that the error is related to some blank spaces I probably had at the end of my file.

  19. VinceSamios
    Member
    Posted 2 years ago #

    Still looking for a solution in the current version of CF7 - I've got half the code in place, just need to dot the i - would be great if someone had the rest of the puzzle - Contact form 7 source url

  20. pfizzi
    Member
    Posted 2 years ago #

    I'm really pleased with this solution, but I can't make it work. I get a CF Validation Error when I send the form. I did everything like described above.

    That's the code in pagelink.php:

    <?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 );
    
    ?>

    That's my form:

    <p>Tu nombre<br />
        [text your-name 20/50] </p>
    
    <p>Tu email<br />
        [email your-email 20/50] </p>
    
    <p>Email de tu amigo<br />
    [email friend-email 20/40] </p>
    
    [pagelink mypagelink] 
    
    <p>Tu mensaje<br />
        [textarea your-message 20x5] </p>
    
    <p>[submit "Enviar"]</p>

    Are the tags inserted correctly like this?

    And finally that's the message body:
    Haz click en este enlace para ir a la pagina: [mypagelink]

    Any ideas?
    Thanks

  21. pfizzi
    Member
    Posted 2 years ago #

    Does it only work with urls that contain the ?id paramater or does it work also with other types of permalinks eg http://www.example.com/category/product/ ?

  22. SimonRain
    Member
    Posted 2 years ago #

    It works perfectly but it made my whole wordpress dashboard stop redirecting when I save about anything. Posts, Pages or contact forms.

    Any tricks to fix it ?

  23. sunnydt
    Member
    Posted 1 year ago #

    Does anyone know how to add the entire url? The url my form comes from has an item number added to it but when it sends it only sends the url like this http://mysite.com/?page_id=224 But I need it to add the full url which would be http://mysite.com/?page_id=224&item=28186 with the item number. Right now it just leaves the item number off the end.

  24. sunnydt
    Member
    Posted 1 year ago #

    nevermind, this is not working out for me. I cannot save anything without an error so I have removed this module. I need something that will insert the item id into the form. Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic