How to set an upload directory for each media type?

[More info added]. See below the code. //~ change upload dir add_filter(‘upload_dir’, ‘choose_upload_dir’); //~ upload image $upload = wp_handle_upload( $_FILES[‘your_form_input_name’], array(‘test_form’ => false) ); //~ change upload dir back remove_filter(‘upload_dir’, ‘choose_upload_dir’); function choose_upload_dir($upload) { //~ your file to upload $file_type = $_FILES[‘your_form_input_name’][‘type’]; //~ switch between file types switch($file_type) { //~ if type is ‘png’ then … Read more

Limit Media Library to Given Folder

A solution that works for me is to add a clause to the WordPress query when the media library is being displayed. From browsing my WordPress database I noticed that the full path to wp_posts.post_type=”attachment” is stored in the wp_posts.guid column. add_filter(‘posts_where’, ‘limitMediaLibraryItems_56456’, 10, 2 ); function limitMediaLibraryItems_56456($where, &$wp_query) { global $pagenow, $wpdb; // Do … Read more

wp_generate_attachment_metadata returns empty array

I believe the following is happening here: As @Luke pointed out, wp_insert_attachment() takes an array of post data. The format of the array you are passing to it is not correct. The keys are different. What should be stored as the post_mime_type, is being passed with the key type. Because of this, no mime type … Read more

Modify featured image path to Amazon S3

You can hook into the output and modify the URL there. add_filter( ‘post_thumbnail_html’, ‘my_post_image_html’, 10, 5 ); function my_post_image_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) { $upload_dir = wp_upload_dir(); $base_url = $upload_dir[‘baseurl’]; // Change the default upload directory to AWS Bucket link $AWSBucket=”http://s3.amazonaws.com/bucket”; $html = str_replace($base_url, $AWSBucket, $html); return $html; } Output the image echo … Read more