• Resolved jackbluehouse2019

    (@jackbluehouse2019)


    Hello from France,
    I am developing a plugin and I would need to upload an image to the wp library from its url. I found some code that uses the function:
    $sideload = wp_handle_sideload(
    but this code generates a critical error on my site.
    I placed the code in the admin part of the plugin and there I have no error but no image is uploaded.
    What can I do
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter jackbluehouse2019

    (@jackbluehouse2019)

    This the code I’m trying to work with:

    
    $image_url= 'https://localhost:8888/chvd//wp-content/plugins/topo-new/topo_multi_zip_2/736192002/DSC08980-1024x683-1.jpeg';
    
    media_upload($image_ur);
    
    function media_upload($image_ur){
    	
    	require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    
    	
    // Utilisez la fonction media_sideload_image pour télécharger l'image depuis l'URL.
    $upload = media_sideload_image($image_url, 0);
    
    // Vérifiez si le téléchargement de l'image a réussi.
    if (!is_wp_error($upload)) {
        // Convertissez l'URL de l'image téléchargée en un ID d'attachement.
        $attachment_id = wp_insert_attachment(array(
            'post_mime_type' => $upload['type'],
            'post_title'     => sanitize_file_name(pathinfo($image_url, PATHINFO_FILENAME)),
            'post_content'   => '',
            'post_status'    => 'inherit'
        ), $upload['file']);
    
        // Générez les données de l'attachement.
        $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload['file']);
        
        // Mettez à jour les données de l'attachement.
        wp_update_attachment_metadata($attachment_id, $attachment_data);
    
        // Insérez l'ID d'attachement dans le contenu du message/page où vous souhaitez afficher l'image.
        // Remplacez "ID_DE_VOTRE_MESSAGE" par l'ID réel de votre message ou page WordPress.
        $post_id = ID_DE_VOTRE_MESSAGE;
        set_post_thumbnail($post_id, $attachment_id);
    }
    
    threadi

    (@threadi)

    Take a close look at how you write the variable $image_ur. Sometimes with l at the end, sometimes without it. There is also a } missing at the very end. And I hope you have set the constant ID_DE_VOTRE_MESSAGE.

    All of this should also be displayed in the error log.

    Thread Starter jackbluehouse2019

    (@jackbluehouse2019)

    Thanks!
    now I found a better code that works:

    
    // Inclure les fichiers nécessaires
    $image_url = 'https://www.bluehouse.fr/wp-content/uploads/2023/11/capture-2023-11-06-a?-08.48.01.jpg';
    
    //upload_image_to_library($image_url );
    
    function upload_image_to_library($image_url ){
    
    // Inclure les fichiers nécessaires
    require_once(ABSPATH . 'wp-includes/pluggable.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    
    // Utilisez la fonction media_sideload_image pour télécharger l'image depuis l'URL.
    $upload = media_sideload_image($image_url, 0);
    
    // Vérifiez si le téléchargement de l'image a réussi.
    if (!is_wp_error($upload)) {
        // L'image a été téléchargée avec succès, et $upload est un tableau contenant les informations sur l'image.
        echo 'Image uploaded successfully!';
    } else {
        // Une erreur s'est produite lors du téléchargement de l'image.
        echo 'Error uploading image: ' . $upload->get_error_message();
    }
    
    
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Insert image in library progamatically’ is closed to new replies.