Currently I am using wp_upload_bits to upload images externally to my website and its working now, however the images are maintaining our source URL as the file name.
$post['image'] = $_POST['image'];
$upload = wp_upload_bits($_POST['image'], null, file_get_contents($_POST['image']));
$filename = $upload['file'];
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => 'cover',
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $hello );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
The above code returns something which looks similar to this within our WordPress site.
How can I solve this, so that I can set my file name via a string?
Change: $filename = $upload['file'];
To: $filename = basename($upload['file']);
Then it will be stored as: llYtuLT-10.jpg
Related
I have a custom php form that upload images to a folder on my website. Currently that folder is outside the wp-content folder.
I want the content of the folder to be automatically uploaded to Google drive. There are plenty of plugins or a zapier solution to do that if the folder is in the wp media folder.
My question is...Can I just upload the images directly to the wp media folder or do I need to use a special wp method/function/api call.
Yes you can upload media directly to the media library. Given your situation media_handle_upload() is probably the most logical way. Can read its documentation here: https://developer.wordpress.org/reference/functions/media_handle_upload/
Take a look at this https://wordpress.stackexchange.com/a/60807/180725
Once you upload the media (to where you want, ftp, dropbox, s3 etc) you will need to register it with Wordpress.
$file_name = 'Some Name';
$file_path = '/path/to/uploads/2012/08/04/newfile.jpg';
$file_url = 'http://url/to/uploads/2012/08/04/newfile.jpg';
$wp_filetype = wp_check_filetype($file, null);
$attachment = array(
'guid' => $file_url,
'post_mime_type' => $wp_filetype['type'],
'post_title' => $file_name,
'post_status' => 'inherit',
'post_date' => date('Y-m-d H:i:s')
);
$attachment_id = wp_insert_attachment($attachment, $file_path);
$attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path);
wp_update_attachment_metadata($attachment_id, $attachment_data);
If you are looking for a solution to upload files to wordpress Mediathek / Library from an external Source/URL, I recommend the following code:
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
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 );
$attachment = array(
'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 );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_ID, $attach_id );
Original post here: https://wordpress.stackexchange.com/a/169598
I'm trying to upload image using wp custom template in child theme but when i upload any image. It appears in "E:\Xamp\htdocs\website\wp-content\uploads\2019\10" but not uploaded to wp dashboard media library.
I'M NOT ALLOWED TO USE ANY PLUGIN FOR THIS TASK.
$post_id = wp_insert_post($my_post);
if(isset($_FILES['file']['name'])){
if(! function_exists('wp_handle_upload')){
require_once(ABSPATH.'wp-admin/includes/file.php');
}
$uploadfile = $_FILES['file'];
print_r($uploadfile);
$upload_overrides = array('test_form' => false );
$moveupload = wp_handle_upload($uploadfile,$upload_overrides);
if($moveupload && ! isset($moveupload['error'])){
echo "</Pre";
wp_update_attachment_metadata( $post_id, $moveupload);
print_r($moveupload);
echo "Post/>";
}else{
echo $moveupload['error'];
}
}
Can try out another piece of code here:
--------------------------
$upload_overrides = array( "test_form" => false );
$uploaded_file = wp_handle_upload ($file, $upload_overrides);
if( isset( $uploaded_file ["file"] )) {
$file_name_and_location = $uploaded_file ["file"];
$file_title_for_media_library = $title;
$attachment = array(
"post_mime_type" => $uploaded_file_type,
"post_title" => addslashes( $file_title_for_media_library ),
"post_content" => "",
"post_status" => "inherit"
);
if( ! is_null( $post )) {
if ( ! is_numeric( $post )) {
$post = $post->ID;
} // if ()
$attachment ['post_parent'] = $post;
} // if ()
$id = wp_insert_attachment( $attachment, $file_name_and_location );
require_once( ABSPATH."wp-admin/includes/image.php" );
$attach_data = wp_generate_attachment_metadata( $id, $file_name_and_location );
wp_update_attachment_metadata( $id, $attach_data );
} // if ()
Uploading files into the wp-content/uploads won't show up in the Media Library , those media ID's needs to be there in the database to show up in the Media Library.
If you already have files in the uploads folder and want to add them into the database
But this is not the correct solution instead fix the permissions issue for that uploads folder.
Thanks
i solved it by using custom php code.
$upload = wp_upload_bits($_FILES["file"]["name"], null, file_get_contents($_FILES["file"]["tmp_name"]));
$filename = $upload['file'];
$wp_filetype = wp_check_filetype($filename, null );
// print_r($filename);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
In this piece of code, an empty file is getting stored in upload folder, but not a proper image file attached. I don't know why.
I'm also not getting the proper usage of wp_upload_bits, it writes a file in upload folder. I don't know how to change that to suite it for the uploading of an image.
I'm totally confused with the error in my code.
I know that there are multiple duplicate question related to it in stack overflow, but I'm not getting a proper result while using the solution described there. Please help me to find a solution.
My piece of code is as follows:
$upload = wp_upload_bits( $_FILES['file']['name'], null, file_get_contents( $_FILES['file']['tmp_name'] ) );
$wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $upload['url'] ,
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename( $upload['file'] )),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta( $post_id, '_thumbnail_id', $attach_id );
set_post_thumbnail( $post_id, $attach_id );
While using this code,attach file is getting stored in WordPress DB, but not in upload folder (media library).
I'm new to WordPress, so I am unable to find a solution for the reason of not storing a proper attached image file in upload folder.
Here is the function that I used to insert attachments to a post.
function insert_entries($filename, $parent_post_id){
$wp_upload_dir = wp_upload_dir();
$filetype = wp_check_filetype( basename( $filename ), null );
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
The issue with this is that, When I am trying to insert any files after inserting a 3gp or m4v file, its not getting inserted. Any insight to solve this is welcome..
Thanks in advance..
Atlast got it fixed.
The reason for the issue was when we try to insert any audio or video files the wordpress tries extract its metadata which was causing for a fatal error.
The simple fix for this is to go to includes/image.php and remove all references to wp_read_video_metadata()
// Auto post ( Unique File Date ).
$postData = array(
'post_category' => array( $Category ),
'post_status' => $Post_Status,
'post_type' => $Post_Type
);
$post_id = wp_insert_post( $postData );
$getImageFile = 'http://localhost/Multisite/test2/wp-content/uploads/sites/4/Auto Post/twitter.png';
$attach_id = wp_insert_attachment( $postData, $getImageFile, $post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $getImageFile );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
The code above make a post successfully but it is not setting the post featured image. I don't know what I am doing wrong here.
Use different $postData for the attachment:
$wp_filetype = wp_check_filetype( $getImageFile, null );
$attachment_data = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $getImageFile ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment_data, $getImageFile, $post_id );
Currently you're passing the identical post data to the post and its attachment post.
I use the function wp_upload_bits is new WordPress function for easy for uploading image. On the second line of my code $post is your $post_id id and for upload file directory I create custom folder that is custom-uploads for your better understanding or you can remove that on my 4th line of coding that is './'. 'custom-uploads' . '/'
$upload = wp_upload_bits($_FILES["file"]["name"], null, file_get_contents($_FILES["file"]["tmp_name"]));
$post_id = $posts; //set post id to which you need to set post thumbnail
$filename = $upload['file'];
$uploadfile = $uploaddir['basedir'] . '/'. 'custom-uploads' . '/';
move_uploaded_file($filename, $uploadfile); // (file name , designation)
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $posts );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id ); // set post thumnail (featured image) for the given post