I created a plugin as 3rd party to acomodate system with wordpress website on main site. So the scenario is:
when user hit submit on the system it will also added to wordpress website, it’s working perfect, no problem at all. but when i try to set the featured image through wp_insert_attachment
it keep me give a URL like
http://xxxxx.com/wp-content/uploads/http://xxxxx.com/system/media/.../xx.jpg
what i want to be is only http://xxxxx.com/system/media/…/xx.jpg saved as featured image, is it possible to do so?
here is my current script
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"]);
}
I have tried to use file_get_contents
and file_put_contents
to create image in WP installation, but I don’t want that way.
The system is submitting this:
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
From
wp_insert_attachment
documentation (my emphasis in bold):Much probably, you can solve this with
media_handle_sideload()
.This function gets the file content using cURL:
This function uses the above to get the image, saves it to the uploads folder, and sets it as a featured image for a post:
Call with: