简体   繁体   中英

Hacking “Contact Form 7” code to Add A “Referred By” field

I've got about 6 subdomains that have a "contact us" link and I'm sending all these links to a single form that uses "Contact Form 7". I add ?from=site-name to each of the links so that I can set a $referredFrom variable in the contact form.

The only two things I'm missing are (1) the ability to insert this referredFrom variable into the email that I get whenever someone submits the form and (2) The ability to redirect the user back to the site they came from (stored in $referredFrom)

Any ideas?

Here's a bit of code from includes/classes.php that I thought might be part of the email insert but its not doing much...

function mail() {
    global $referrer;
    $refferedfrom = $referrer; //HERE IS MY CUSTOM CODE
    $fes = $this->form_scan_shortcode();

    foreach ( $fes as $fe ) {
        $name = $fe['name'];
        $pipes = $fe['pipes'];

        if ( empty( $name ) )
            continue;

        $value = $_POST[$name];

        if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
            if ( is_array( $value) ) {
                $new_value = array();
                foreach ( $value as $v ) {
                    $new_value[] = $pipes->do_pipe( $v );
                }
                $value = $new_value;
            } else {
                $value = $pipes->do_pipe( $value );
            }
        }

        $this->posted_data[$name] = $value;
        $this->posted_data[$refferedfrom] = $referrer; //HERE IS MY CUSTOM CODE
    }

I'm also thinking that I could insert the referredFrom code somewhere in this function as well...

function compose_and_send_mail( $mail_template ) {
    $regex = '/\[\s*([a-zA-Z][0-9a-zA-Z:._-]*)\s*\]/';
    $callback = array( &$this, 'mail_callback' );

    $mail_subject = preg_replace_callback( $regex, $callback, $mail_template['subject'] );
    $mail_sender = preg_replace_callback( $regex, $callback, $mail_template['sender'] );
    $mail_body = preg_replace_callback( $regex, $callback, $mail_template['body'] );
    $mail_recipient = preg_replace_callback( $regex, $callback, $mail_template['recipient'] );

    $mail_headers = "From: $mail_sender\n";

    if ( $mail_template['use_html'] )
        $mail_headers .= "Content-Type: text/html\n";

    $mail_additional_headers = preg_replace_callback( $regex, $callback,
        $mail_template['additional_headers'] );
    $mail_headers .= trim( $mail_additional_headers ) . "\n";

    if ( $this->uploaded_files ) {
        $for_this_mail = array();
        foreach ( $this->uploaded_files as $name => $path ) {
            if ( false === strpos( $mail_template['attachments'], "[${name}]" ) )
                continue;
            $for_this_mail[] = $path;
        }

        return @wp_mail( $mail_recipient, $mail_subject, $mail_body, $mail_headers,
            $for_this_mail );
    } else {
        return @wp_mail( $mail_recipient, $mail_subject, $mail_body, $mail_headers );
    }
}

I'd found a plugin that works fantastic for doing this, plus a little more:

http://wordpress.org/plugins/contact-form-7-leads-tracking/

Which will add all the information to the end of your email when it is sent

First of all, in order to get the from variable you'll have to insert

$referrer = $_GET['from'];

somewhere in the top script, at least before the last line you inserted.

Additionally, in the second script you have to add the value to $mail_body somehow, but since I don't know how that value is made up I can't help much with that. Is the code for this form available online somewhere?

Insert in your functions.php or create a simple plugin...

1.

function custom_wpcf7_special_mail_tag( $output, $name  ) {
    if ( 'from' == $name ) {
        $referredFrom = ( isset($_GET["from"]) && !empty($_GET["from"]) ) ? $_GET["from"] : '';
        $output =  $referredFrom;
    }

    return $output;
}
add_filter( 'wpcf7_special_mail_tags', 'custom_wpcf7_special_mail_tag', 10, 2 );

Use the [from] tag in your email template.

2.

function add_custom_js_cf7() {
    $referredFrom = ( isset($_GET["from"]) && !empty($_GET["from"]) ) ? $_GET["from"] : '';
    if ( $referredFrom ) {
    ?>
        <script type="text/javascript">
        var from = "<?php echo $referredFrom; ?>";
        </script>
<?php }
}
add_action( 'wpcf7_enqueue_scripts', 'add_custom_js_cf7' );

And add this line to the "additional settings" in your form settings:

on_sent_ok: "location = from;"

http://contactform7.com/blog/2010/03/27/redirecting-to-another-url-after-submissions/


You can also use global $referredFrom; if you declared it somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM