2

我创建了一个插件作为第 3 方,以便在主站点上使用 wordpress 网站来适应系统。所以场景是:当用户在系统上点击提交时,它也会添加到 wordpress 网站,它工作完美,完全没有问题。但是当我尝试通过wp_insert_attachment它设置特色图片时,请让我给出一个类似的 URL

http://xxxxx.com/wp-content/uploads/http://xxxxx.com/system/media/.../xx.jpg

我想要的只是http://xxxxx.com/system/media/.../xx.jpg保存为特色图片,可以这样做吗?这是我当前的脚本

if($pt == "pictures"){
        $filename_url = $_GET["dml_file"];
        $mime = wp_check_filetype($filename_url, null);
        $data = array(
         'post_mime_type' => $mime['type'],
         'post_title' => preg_replace('/\.[^.]+$/', '', basename($_GET["dml_file"])),
         'post_content' => '',
         'post_status' => 'inherit'
        );
        $attachment_id = wp_insert_attachment($data, $filename_url, $pid);
        update_post_meta($pid, $custom_field, $attachment_id);
      }else{
        update_post_meta($pid, $custom_field, $_GET["dml_file"]);
      }

我曾尝试在 WP 安装中使用file_get_contentsfile_put_contents创建图像,但我不希望那样。

系统提交这个:

http://user:pass!@localhost/wp-content/plugins/dml3rdparty/dmlsubmit.php?dml_sa‌ve=save&dml_file=http://xxx.xxx.xxx.xxx/dml/assets/media/Accreditation/download.d‌15bdf4e9e.jpg&dml_type=Print Quality Photos&dml_description=test|download.jpg&dml_status=publish
4

2 回答 2

2

来自wp_insert_attachment 文档(我的重点是粗体):

$filename
(字符串)(可选)文件在服务器上的位置。使用绝对路径而不是文件的 URI。该文件必须在上传目录中。请参阅 默认值:falsewp_upload_dir()

很可能,您可以使用media_handle_sideload().

于 2013-10-05T13:52:53.533 回答
0

此函数使用 cURL 获取文件内容:

function my_file_get_contents($url){
    $options = array(
        CURLOPT_AUTOREFERER => true,
        CURLOPT_HEADER => false,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_CONNECTTIMEOUT => 120,
        CURLOPT_TIMEOUT => 120,
        CURLOPT_MAXREDIRS => 10
    );
    $ch = curl_init($url);
    curl_setopt_array($ch,$options);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

此函数使用上述方法获取图像,将其保存到上传文件夹,并将其设置为帖子的特色图像:

function my_featured_image($image_url,$post_id){
    $upload_dir = wp_upload_dir();
    $image_data = my_file_get_contents($image_url);
    $filename = strtok($image_url, '?');
    $filename = basename($filename);
    if(wp_mkdir_p($upload_dir["path"])){
        $file = $upload_dir["path"]."/".$filename;
    }else{
        $file = $upload_dir["basedir"]."/".$filename;
    }
    file_put_contents($file, $image_data);
    $wp_filetype = wp_check_filetype($filename,null);
    $post_author = get_post_field("post_author",$post_id);
    $attachment = array(
        "post_author" => $post_author,
        "post_mime_type" => $wp_filetype["type"],
        "post_title" => sanitize_file_name($filename),
        "post_content" => "",
        "post_status" => "inherit"
    );
    $attach_id = wp_insert_attachment($attachment,$file,$post_id);
    require_once(ABSPATH."wp-admin/includes/image.php");
    $attach_data = wp_generate_attachment_metadata($attach_id,$file);
    $res1 = wp_update_attachment_metadata($attach_id,$attach_data);
    $res2 = set_post_thumbnail($post_id, $attach_id);
}

致电:

my_featured_image($image_url,$post_id);
于 2016-02-09T17:11:37.047 回答