Guest User

Untitled

a guest
Feb 7th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Contact Form 7 to External DB
  4. Plugin URI:
  5. Description: This plugin uses the wpcf7_before_send_mail hook to post a specific form to an external database. Upon use the details for your external database will need to be entered. Private use only.
  6. Author:
  7. Version: 0.2
  8. Author URI:
  9. */
  10.  
  11. function wpcf7_send_to_external ( $cf7 ) {
  12.  
  13. //external db details
  14. $username = 'username';
  15. $password = 'password';
  16. $database = 'database';
  17. $host = 'host_ip';
  18.  
  19. //create new wpdb instance
  20. $mydb = new wpdb($username, $password, $database, $host);
  21.  
  22. //limit hook to only fire on particular form ID (optional)
  23. if ( $cf7->id == 1 ) {
  24.  
  25. //get select box for different enquiry types (optional)
  26. $type = $cf7->posted_data["your-select"];
  27.  
  28. //if form type is equal to above value (optional)
  29. if ( $type == 'Form Name' ){
  30.  
  31. //code added for wordpress 4.9 2018
  32. $cf7 = WPCF7_ContactForm::get_current();
  33. $submission = WPCF7_Submission::get_instance();
  34. $data = $submission->get_posted_data();
  35.  
  36.  
  37. //get posted form fields
  38. //these are example form fields
  39. $field1 = $data["name"];
  40. $field2 = $data["email"];
  41. $field3 = $data["address"];
  42. $field4 = $data["city"];
  43. $field5 = $data["state"];
  44. $field6 = $data["zip"];
  45. $field7 = $data["phone"];
  46.  
  47.  
  48. //insert into external db
  49. $mydb->insert(
  50. //name of external db table
  51. 'table_name',
  52. //name of table columns and which fields to insert
  53. //these are example fields
  54. array(
  55. 'name' => $field1,
  56. 'email' => $field2,
  57. 'address' => $field3,
  58. 'city' => $field4,
  59. 'state' => $field5,
  60. 'zip' => $field6,
  61. 'phone' => $field7
  62.  
  63. ),
  64. //field formats: %s = string, %d = integer, %f = float
  65. array(
  66. '%s','%s','%s','%s','%s','%s','%s'
  67. )
  68. );
  69.  
  70. }
  71.  
  72. }
  73.  
  74. }
  75. add_action("wpcf7_before_send_mail", "wpcf7_send_to_external");
Add Comment
Please, Sign In to add comment