Created
December 4, 2025 22:37
-
-
Save Oliviercreativ/ae84d38bf6f1b42e06a1544757f62557 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Hook simple pour ajouter une image à la une via URL | |
| * Ajouter ce code dans functions.php ou un fichier must-use plugin | |
| */ | |
| // Ajouter la métabox | |
| add_action('add_meta_boxes', function() { | |
| add_meta_box( | |
| 'featured_image_url', | |
| 'Image à la une (URL)', | |
| function($post) { | |
| wp_nonce_field('fiu_nonce', 'fiu_nonce'); | |
| $url = get_post_meta($post->ID, '_featured_image_url', true); | |
| ?> | |
| <p> | |
| <input type="text" name="featured_image_url" value="<?php echo esc_attr($url); ?>" placeholder="https://example.com/image.jpg" style="width: 100%;" /> | |
| </p> | |
| <button type="button" class="button" onclick="setFeaturedImageFromUrl()">Définir comme image à la une</button> | |
| <script> | |
| function setFeaturedImageFromUrl() { | |
| const url = document.querySelector('input[name="featured_image_url"]').value; | |
| if (!url) { | |
| alert('Entrez une URL'); | |
| return; | |
| } | |
| fetch('<?php echo admin_url('admin-ajax.php'); ?>', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/x-www-form-urlencoded'}, | |
| body: 'action=set_featured_image_url&post_id=<?php echo $post->ID; ?>&url=' + encodeURIComponent(url) + '&nonce=<?php echo wp_create_nonce('fiu_nonce'); ?>' | |
| }) | |
| .then(r => r.json()) | |
| .then(data => { | |
| if (data.success) { | |
| alert('Image définie !'); | |
| location.reload(); | |
| } else { | |
| alert('Erreur: ' + data.data.message); | |
| } | |
| }); | |
| } | |
| </script> | |
| <?php | |
| }, | |
| array('post', 'page', 'product'), | |
| 'side', | |
| 'high' | |
| ); | |
| }); | |
| // Sauvegarder l'URL | |
| add_action('save_post', function($post_id) { | |
| if (!isset($_POST['fiu_nonce']) || !wp_verify_nonce($_POST['fiu_nonce'], 'fiu_nonce')) return; | |
| if (isset($_POST['featured_image_url'])) { | |
| update_post_meta($post_id, '_featured_image_url', sanitize_url($_POST['featured_image_url'])); | |
| } | |
| }); | |
| // AJAX : Importer et définir l'image | |
| add_action('wp_ajax_set_featured_image_url', function() { | |
| check_ajax_referer('fiu_nonce', 'nonce'); | |
| $post_id = intval($_POST['post_id']); | |
| $url = sanitize_url($_POST['url']); | |
| if (!current_user_can('edit_post', $post_id) || empty($url)) { | |
| wp_send_json_error(array('message' => 'Erreur')); | |
| } | |
| // Télécharger l'image | |
| $response = wp_remote_get($url, array('timeout' => 30, 'sslverify' => false)); | |
| if (is_wp_error($response)) { | |
| wp_send_json_error(array('message' => 'Impossible de télécharger')); | |
| } | |
| $image_data = wp_remote_retrieve_body($response); | |
| $filename = basename(parse_url($url, PHP_URL_PATH)) ?: 'image-' . time() . '.jpg'; | |
| // Sauvegarder le fichier | |
| $upload_dir = wp_upload_dir(); | |
| $file_path = $upload_dir['path'] . '/' . $filename; | |
| if (file_put_contents($file_path, $image_data) === false) { | |
| wp_send_json_error(array('message' => 'Impossible de sauvegarder')); | |
| } | |
| // Créer l'attachment | |
| $attachment_id = wp_insert_attachment(array( | |
| 'post_mime_type' => mime_content_type($file_path), | |
| 'post_title' => sanitize_file_name(pathinfo($filename, PATHINFO_FILENAME)), | |
| 'post_status' => 'inherit' | |
| ), $file_path, $post_id); | |
| if (is_wp_error($attachment_id)) { | |
| wp_send_json_error(array('message' => 'Erreur création')); | |
| } | |
| // Générer les métadonnées et définir comme image à la une | |
| wp_generate_attachment_metadata($attachment_id, $file_path); | |
| set_post_thumbnail($post_id, $attachment_id); | |
| wp_send_json_success(array('message' => 'Image définie !')); | |
| }); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment