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' );
media_sideload_image()
.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' );