Insert image in library progamatically

[ad_1]

This the code I’m trying to work with:


$image_url="http://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);
}

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.

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-à-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();
}


}

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer