3

I display images on my site from other sites. The URL is saved in a custom field named couv.

I would like to download these pictures on to my site and create a new custom field with the new value couv2.

I tried to do this with the following code in my functions.php file but it doesn't work.

function downloadcouv() {
    global $wp_query;
    $lien = get_post_meta(
        $wp_query->queried_object->ID,
        "couv",
        true
    );
    $uploads = wp_upload_dir();
    $reg = "#([^/]*)$#";
    $chaine = $lien;
    preg_match( $reg, $chaine, $res );
    $couv = $uploads['baseurl'] . '/couv/' . $res[1];
    copy( $lien, $couv );
    add_post_meta(
        $wp_query->queried_object->ID,
        "couv2",
        $couv,
        true
    );
}
add_action( 'save_post', 'downloadcouv' );

Edit: I made this change but it is still not working.

function downloadcouv() {
    global $wp_query;
    $post_id = $wp_query->queried_object->ID;
    $lien = get_post_meta(
        $post_id,
        "couv",
        true
    );
    $desc = $post_title; 
    $couv = media_sideload_image(
        $lien,
        $post_id,
        $desc
    );
    add_post_meta(
        $post_title,
        "couv2",
        $couv,
        true
    );
}
add_action( 'save_post', 'downloadcouv' );
6
  • 2
    Take a look at media_sideload_image().
    – kaiser
    Commented May 13, 2016 at 12:59
  • I change it like this, but no way... function downloadcouv() { global $wp_query; $post_id = $wp_query->queried_object->ID; $lien = get_post_meta($post_id, "couv", true); $desc = $post_title; $couv = media_sideload_image($lien, $post_id, $desc); add_post_meta($post_title, "couv2", $couv, true); } add_action( 'save_post', 'downloadcouv' );
    – Guenfood
    Commented May 13, 2016 at 13:14
  • media_sideload_image() is to download an image from the specified URL and attach it to a post. But i don't want to use this image as a featured image. Just display it in post_id sidebar. I've got another image for featured image.
    – Guenfood
    Commented May 13, 2016 at 14:04
  • 1
    Please add additional efforts as edits to your question. That's not readable as comment.
    – kaiser
    Commented May 13, 2016 at 20:25
  • 1
    I fixed the code readability for you. Please take this as example for further questions – the easier to read, the better and more answers you will get. Aside from that, "I change it like this, but no way…" is hardly an error description. No one sees your screen or will replicate your setup. Please be a bit more verbose. Just imagine you know nothing about your problem, then read your question and try to think what you understand.
    – kaiser
    Commented May 14, 2016 at 14:34

1 Answer 1

0

Here is a solution to upload a copy of the image saved as a URL in a custom field into a local directory on your site. This will not add the image to the media library and is meant as more of a caching mechanism.

You should always be sure that you have permission to use images from other sources before you use them. Only use this code for good!

function downloadcouv( $post_id, $post, $update ) {

    // Update the post's metadata.

    if ( isset( $_POST['couv'] ) ) {

        // Get the upload directory
        $upload_dir = wp_upload_dir();

        // Make sure that the image URL is actually a URL
        if ( FALSE !== filter_var($_POST['couv'], FILTER_VALIDATE_URL) ) {

            // Get the file extension
            $ext = pathinfo($_POST['couv'], PATHINFO_EXTENSION);

            // Copy the file from the url to a local directory
            copy( $backdrop, $upload_dir['basedir'] . '/site-img-' . $post_id . '.' . $ext );

            // Update the post meta with the URL to the uploaded image
            update_post_meta( $post_id, 'couv2', $upload_dir['basedir'] . '/site-img-' . $post_id . '.' . $ext );

        }

    }

}
add_action( 'save_post', 'downloadcouv', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.