plugin development - How to Upload Images From a Folder (Not WP_Uploads)

Everything I am passing into the function exists but no images are being attached. The images are not in the WP_uploads

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

相关推荐

  • plugin development - How to Upload Images From a Folder (Not WP_Uploads)

    Everything I am passing into the function exists but no images are being attached. The images are not in the WP_uploads

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信