Actually, iI display images on my site from other sites. UrlThe URL is onsaved in a custom field name "couv"
I'dnamed couv
.
I would like to download thesesthese pictures on to my site and create a new custom field with the new value "couv2"
Icouv2
.
I tried to do this with a function in my functions.php with the following code.
But in my functions.php
file but it doesn't work.
Where did i make a mistake ?
Thanks for your help.
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 change it likemade this, change but no way..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' );