<?php | |
$file = '/path/to/file.png'; | |
$filename = basename($file); | |
$upload_file = wp_upload_bits($filename, null, file_get_contents($file)); | |
if (!$upload_file['error']) { | |
$wp_filetype = wp_check_filetype($filename, null ); | |
$attachment = array( | |
'post_mime_type' => $wp_filetype['type'], | |
'post_parent' => $parent_post_id, | |
'post_title' => preg_replace('/\.[^.]+$/', '', $filename), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $parent_post_id ); | |
if (!is_wp_error($attachment_id)) { | |
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); | |
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] ); | |
wp_update_attachment_metadata( $attachment_id, $attachment_data ); | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
great codee |
This comment has been minimized.
This comment has been minimized.
Not working. Fatal error: Call to undefined function wp_get_current_user() in /home/username/public_html/wp/wp-includes/capabilities.php on line 424 |
This comment has been minimized.
This comment has been minimized.
Perfect! Thank you! |
This comment has been minimized.
This comment has been minimized.
Just registred to say thank you - this is indeed working great with latest Wordpress! You're the best, hissy! |
This comment has been minimized.
This comment has been minimized.
Great code! Working perfect! |
This comment has been minimized.
This comment has been minimized.
Just used with WP 4.5.2 for a custom migration script and it works fine. |
This comment has been minimized.
This comment has been minimized.
how do I fill the $parent_post_id variable? if I put this code in function.php, and I will upload image like using the uploader in wp-admin/media-new.php |
This comment has been minimized.
This comment has been minimized.
Great one, works perfect, Thanks a lot :) |
This comment has been minimized.
This comment has been minimized.
Ty Mate, I have been looking for ages to get stuff working properly. Now it does! |
This comment has been minimized.
This comment has been minimized.
Perfect. |
This comment has been minimized.
This comment has been minimized.
Nice man. it helps too much. |
This comment has been minimized.
This comment has been minimized.
It's perfect.... |
This comment has been minimized.
This comment has been minimized.
thank you very much |
This comment has been minimized.
This comment has been minimized.
works great! |
This comment has been minimized.
This comment has been minimized.
This is fantastic! Thank you!! |
This comment has been minimized.
This comment has been minimized.
This is just a code from WORDPRESS CODEX page... so lol... ppl dont like to read documentation... |
This comment has been minimized.
This comment has been minimized.
Great script, Thanks Man. Thank you so much. |
This comment has been minimized.
This comment has been minimized.
@qant This is NOT. The key point of this gist is using |
This comment has been minimized.
This comment has been minimized.
Worked great. Thanks! |
This comment has been minimized.
This comment has been minimized.
It worked successfully! Thank you for this sample code! |
This comment has been minimized.
This comment has been minimized.
Thanks for the snippet! Works great. In case anyone gets the following order: Check out following post: https://stackoverflow.com/a/6127607 |
This comment has been minimized.
This comment has been minimized.
awesome stuff |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
Still works in 5.2, thanks for saving me a bunch of time! |
This comment has been minimized.
This comment has been minimized.
Hi guys, Thanks |
This comment has been minimized.
This comment has been minimized.
@oadekoya12 I think you can set the |
This comment has been minimized.
This comment has been minimized.
@kensington133, |
This comment has been minimized.
This comment has been minimized.
Using this code for MP3s. Works great! Any ideas how to get the Post ID of the newly created media to add a featured image? |
This comment has been minimized.
This comment has been minimized.
@thegreentimtam |
This comment has been minimized.
This comment has been minimized.
Hey - been using this for a while and it works great. However I have a new requirement where I just want to import images to Media Library, but NOT set as attachment to particular post. Is that possible? All the functions here require a |
This comment has been minimized.
This comment has been minimized.
Sorry - just read the docs for https://codex.wordpress.org/Function_Reference/wp_insert_attachment |
This comment has been minimized.
This comment has been minimized.
Yup, you don't have to pass |
This comment has been minimized.
This comment has been minimized.
This works consistently as long as you're not waiting for the reply. I am using a rest endpoint to generate products from a remote source and using a method link this to create the images. I have found if you have more than a few media sizes this can easily risk timing out the web-server if it has an anywhere near reasonable wait time. A site I'm using has 28 different media sizes and we never manage to run the wp_generate_attachment_metadata and get a reply before the timeout is reached. Annoyingly our service then assumes failure and tries again, and again and again. Eventually crashing the server and leaving us with hundreds of duplicate images! Just recording this here as its top of google for timeouts regarding the use of this function. |
This comment has been minimized.
This comment has been minimized.
@srjwebster |
This comment has been minimized.
This comment has been minimized.
@fabriziofeitosa I found that by removing some unused media sizes, and by turning off image optimisation plugin wp smush I could run this n a much shorter timeframe. A few seconds compared to several minutes. Definitely not a solution, something asynchronous seems necessary to do this on a frequent basis. |
This comment has been minimized.
This comment has been minimized.
Script is very good and its worked. Thanks!! |
This comment has been minimized.
This comment has been minimized.
Life Saver |
This comment has been minimized.
This comment has been minimized.
Thanks a lot, your code is very good. |
This comment has been minimized.
This comment has been minimized.
It's worked on WP 5.6! |
This comment has been minimized.
Great script. I'm trying to insert an alt tag as well right away. How do you recommend accomplishing that?