Skip to content

Instantly share code, notes, and snippets.

@zxcnasab
Last active July 27, 2020 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zxcnasab/b3054dd6c05fcd5d42576cfb734a60fc to your computer and use it in GitHub Desktop.
Save zxcnasab/b3054dd6c05fcd5d42576cfb734a60fc to your computer and use it in GitHub Desktop.
Modify CF7 Data Before Submission
<?php
add_action( 'wpcf7_before_send_mail', 'wpcf7_custom_data', 90, 1 );
function wpcf7_custom_data( $WPCF7_ContactForm ){
// Submission object, that generated when the user click the submit button.
$submission = WPCF7_Submission :: get_instance();
if ( $submission ){
$posted_data = $submission->get_posted_data();
if ( empty( $posted_data ) ){ return; }
// Got ip data
$ip_data = $posted_data['ip'];
$ipv6 = $_SERVER['REMOTE_ADDR'];
// Got e-mail text
$mail = $WPCF7_ContactForm->prop( 'mail' );
// Replace "[ip]" field inside e-mail text
$new_mail = str_replace( '[ip]', $ipv6, $mail );
// Set
$WPCF7_ContactForm->set_properties( array( 'mail' => $new_mail ) );
return $WPCF7_ContactForm;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment