Skip to main content
Updated readability and clarified the question
Source Link

Function to Download external images with functionExternal Images to My Site

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' );

Download external images with function

Actually, i display images on my site from other sites. Url is on a custom field name "couv" I'd like to download theses pictures on my site and create a new custom field with the new value "couv2" I tried to do this with a function in my functions.php with the following code. 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 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' );

Function to Download External Images to My Site

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' );
Tweeted twitter.com/StackWordPress/status/838759827627196421
Fixes Readability
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

Actually, i display images on my site from other sites. Url is on a custom field name "couv" I'd like to download theses pictures on my site and create a new custom field with the new value "couv2" I tried to do this with a function in my functions.php with the following code. But it doesn't work. Where did i make a mistake ? Thanks for your help.

    function downloadcouv() {
    global $wp_query;
$lien=get_post_meta    $lien = get_post_meta(
        $wp_query->queried_object->ID,
        "couv",
        true
    );
    $uploads = wp_upload_dir();
$reg="#    $reg = "#([^/]*)$#";
$chaine=$lien;    $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 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' );

Actually, i display images on my site from other sites. Url is on a custom field name "couv" I'd like to download theses pictures on my site and create a new custom field with the new value "couv2" I tried to do this with a function in my functions.php with the following code. 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 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' );

Actually, i display images on my site from other sites. Url is on a custom field name "couv" I'd like to download theses pictures on my site and create a new custom field with the new value "couv2" I tried to do this with a function in my functions.php with the following code. 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 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' );
added 388 characters in body
Source Link
Guenfood
  • 61
  • 1
  • 5

Actually, i display images on my site from other sites. Url is on a custom field name "couv" I'd like to download theses pictures on my site and create a new custom field with the new value "couv2" I tried to do this with a function in my functions.php with the following code. 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 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' );

Actually, i display images on my site from other sites. Url is on a custom field name "couv" I'd like to download theses pictures on my site and create a new custom field with the new value "couv2" I tried to do this with a function in my functions.php with the following code. 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' );

Actually, i display images on my site from other sites. Url is on a custom field name "couv" I'd like to download theses pictures on my site and create a new custom field with the new value "couv2" I tried to do this with a function in my functions.php with the following code. 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 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' );
Source Link
Guenfood
  • 61
  • 1
  • 5
Loading