Everything I am passing into the function exists but no images are being attached. The images are not in the WP_uploads folder, they are in a seperate folder in the home directory. This is executed inside of a plugin.
function insert_attachment_image($url, $parent_post_id, $thumbnail){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$tmp = download_url($url);
$file_array = array(
'name' => basename($url),
'tmp_name' => $tmp
);
if (is_wp_error($tmp)){
@unlink($file_array['tmp_name']);
return $tmp;
}
$id = media_handle_upload($file_array, $parent_post_id);
if (is_wp_error($id)){
@unlink($file_array['tmp_name']);
return false;
}
if($thumbnail){
set_post_thumbnail( $parent_post_id, $id);
}
$value = wp_get_attachment_url($id); // file url
return $value;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272971a4619862.html
评论列表(0条)