1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?php
// Intercepter les soumissions de formulaire Contact Form 7
function openai_cf7_form_submitted($cf7) {
// Récupérer le fichier d'image
$uploaded_image = isset($_FILES['image1']) ? $_FILES['image1'] : '';
// Vérifier si un fichier d'image a été téléchargé
if (!empty($uploaded_image) && $uploaded_image['error'] === 0) {
// Insérer l'image dans la bibliothèque des médias
$attachment_id = insert_image_to_media_library($uploaded_image);
// Vérifier si l'insertion de l'image a réussi
if ($attachment_id) {
// Mettre à jour le champ personnalisé ACF avec l'ID de l'attachement
$image_field_name = 'ai02'; // Remplacez par le nom de votre champ image
update_field($image_field_name, array($attachment_id), 'options');
}
}
error_log('Le formulaire a été soumis');
error_log('Données soumises : ' . print_r($_POST, true));
}
add_action("wpcf7_before_send_mail", "openai_cf7_form_submitted");
function insert_image_to_media_library($uploaded_image) {
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/' . basename($uploaded_image['name']);
if (move_uploaded_file($uploaded_image['tmp_name'], $file_path)) {
$attachment = array(
'post_mime_type' => $uploaded_image['type'],
'post_title' => sanitize_file_name($uploaded_image['name']),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $file_path);
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path);
wp_update_attachment_metadata($attachment_id, $attachment_data);
return $attachment_id;
}
}
error_log("Erreur lors du téléchargement de l'image dans la bibliothèque des médias.");
return false;
} |
Partager