Advertisement
Guest User

Untitled

a guest
May 3rd, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | Help | 0 0
  1.  
  2. <?php
  3. /**
  4. * Plugin Name: Forminator ➜ Voxel Sync (with CSV Mapping)
  5. * Description: Automatically transfers Forminator data to a "place" post type (GetVoxel), using an external mapping.
  6. */
  7.  
  8. add_action('forminator_custom_form_after_save_entry', function ($entry_id, $form_id) {
  9. if ($form_id == 809) {
  10. if (!class_exists('Forminator_Form_Entry_Model')) {
  11. return;
  12. }
  13.  
  14. $entry = Forminator_Form_Entry_Model::get_instance($entry_id);
  15. if (!$entry) {
  16. return;
  17. }
  18.  
  19. $fields = $entry->get_fields();
  20. $data = [];
  21.  
  22. // Retrieve data with their IDs
  23. foreach ($fields as $field) {
  24. $data[$field['name']] = $field['value']; // The 'name' field corresponds to the Field ID
  25. }
  26.  
  27. // Use a relevant field as the title
  28. $post_title = sanitize_text_field($data['text-16'] ?? 'New listing'); // Example: text-16 should be adapted to the actual mapping
  29.  
  30. // Author based on email, or current user
  31. $author_email = sanitize_email($data['email-1'] ?? ''); // Adapt to actual mapping
  32. $user = get_user_by('email', $author_email);
  33. $author_id = $user ? $user->ID : get_current_user_id();
  34.  
  35. $new_post = [
  36. 'post_title' => $post_title,
  37. 'post_status' => 'publish',
  38. 'post_type' => 'place',
  39. 'post_author' => $author_id,
  40. ];
  41.  
  42. $post_id = wp_insert_post($new_post);
  43.  
  44. if ($post_id && !is_wp_error($post_id)) {
  45. // Actual mapping from the CSV file
  46. $mapping = [
  47. 'hidden-1' => 'submission_ID',
  48. 'hidden-3' => 'submission_date',
  49. 'hidden-4' => 'submission_time',
  50. 'radio-2' => 'creditor_profile',
  51. 'select-7' => 'creditor_individual_title',
  52. // ... Add other fields here as in the file
  53. ];
  54.  
  55. foreach ($mapping as $field_id => $meta_key) {
  56. if (!empty($data[$field_id])) {
  57. update_post_meta($post_id, $meta_key, sanitize_text_field($data[$field_id]));
  58. }
  59. }
  60. }
  61. }
  62. }, 10, 2);
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement