Skip to content

Instantly share code, notes, and snippets.

@yeriepiscesa
Last active April 20, 2019 04:32
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 yeriepiscesa/0acde5b4876dc61730e8f3c41defe49f to your computer and use it in GitHub Desktop.
Save yeriepiscesa/0acde5b4876dc61730e8f3c41defe49f to your computer and use it in GitHub Desktop.
<?php
function wpcf7_contacts_save_to_db( $cf, $result ) {
$submission = WPCF7_Submission::get_instance();
if ( ! $submission || ! $posted_data = $submission->get_posted_data() ) {
return;
}
if( $result['status'] == 'mail_sent' ) {
global $wpdb;
if( !is_numeric( $posted_data['province'] ) ) $posted_data['province'] = null;
if( !is_numeric( $posted_data['regency'] ) ) $posted_data['regency'] = null;
if( !is_numeric( $posted_data['district'] ) ) $posted_data['district'] = null;
if( !is_numeric( $posted_data['village'] ) ) $posted_data['village'] = null;
$values = array(
'full_name' => $posted_data['your-name'],
'email' => $posted_data['your-email'],
'address' => $posted_data['your-address'],
'province_id' => $posted_data['province'],
'regency_id' => $posted_data['regency'],
'district_id' => $posted_data['district'],
'village_id' => $posted_data['village'],
'subject' => $posted_data['your-subject'],
'message' => $posted_data['your-message'],
'ip_address' => $_SERVER['REMOTE_ADDR'],
'submit_date' => date("Y-m-d H:i:s"),
'user_agent' => $_SERVER['HTTP_USER_AGENT']
);
$inserted = $wpdb->insert(
$wpdb->prefix.'contacts',
$values,
array(
'%s', '%s', '%s',
'%s', '%s', '%s', '%s',
'%s', '%s',
'%s', '%s', '%s'
)
);
}
}
add_action( 'wpcf7_submit', 'wpcf7_contacts_save_to_db', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment