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' );
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' );
Share
Improve this question
edited Jun 12, 2019 at 19:28
Marc
7415 silver badges15 bronze badges
asked May 13, 2016 at 12:41
GuenfoodGuenfood
611 silver badge5 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 0Here 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 );
media_sideload_image()
. – kaiser Commented May 13, 2016 at 12:59function 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