9

I decided to approach it differently. Instead of hooking into 'wp_handle_upload_prefilter' and tampering with the $file variable I decided to do the resize after the file is uploaded and after I get the attachment id like this: public function resize_attachment($attachment_id, $width, $height) { // Get file path $file = get_attached_file($...


8

Got it, looking at the source code I came up with this: wp_handle_upload($file_input, array('test_form' => false, 'mimes' => array('csv' => 'text/csv'))); To override the mime types just pass mimes as an array wit the key being the file extension and the value as the mime type.


8

You can work on the idea that Joost provided and use the upload_dir filter to temporarily set the upload path to somewhere else. /** * Override the default upload path. * * @param array $dir * @return array */ function wpse_141088_upload_dir( $dir ) { return array( 'path' => $dir['basedir'] . '/mycustomdir', 'url' =>...


8

The better title for this would be: How to use the WordPress media uploader to upload images into settings page. 1- Add the necessary scripts to show the media uploader: add_action('admin_footer', function() { /* if possible try not to queue this all over the admin by adding your settings GET page val into next if( empty( $_GET['page'] ) || "...


7

Don't store user uploads in your theme folder! I would suggest you store them in a sub-directory of the default uploads folder. You can use the upload_dir filter to temporarily change the path: function wpse_183245_upload_dir( $dirs ) { $dirs['subdir'] = '/abc'; $dirs['path'] = $dirs['basedir'] . '/abc'; $dirs['url'] = $dirs['baseurl'] . '/abc';...


5

Here's a complete example for how we do it in Easy Digital Downloads: /** * Set Upload Directory * * Sets the upload dir to edd. This function is called from * edd_change_downloads_upload_dir() * * @since 1.0 * @return array Upload directory information */ function edd_set_upload_dir( $upload ) { $upload['subdir'] = '/edd' . $upload['subdir']; ...


5

The move_uploaded_file function is a PHP function: http://php.net/manual/en/function.move-uploaded-file.php One important thing to note about it from that page: This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will ...


4

May be you are not returning correctly from function, Try this, function insert_attachment($file_handler, $post_id, $setthumb=false) { // check to make sure its a successful upload // changes start if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) { return __return_false(); } // changes end require_once(ABSPATH . "wp-...


4

here's my example of how to upload multiple images: add_action( 'add_meta_boxes', 'my_test_metabox' ); function my_test_metabox() { add_meta_box( 'my_test_metabox', 'File upload', 'my_test_metabox_out', 'post' ); } add_action('post_edit_form_tag', 'update_edit_form' ); function update_edit_form() { echo ' enctype="multipart/form-data"'; } ...


4

You could e.g. check the filename and extension from the pathinfo, after your custom sanitization. Example: If the filename is empty and extension non-empty, then add the formatted current time as the filename part: $info = pathinfo( $file['name'] ); if( empty( $info['filename'] ) && ! empty( $info['extension'] ) ) $file['name'] = sprintf( '%s....


4

I think the proper filter would be mime_types found here. function wpse294198_mime_types( $mimes ) { $mimes['xls|xlsx'] = 'application/vnd.ms-excel'; return $mimes; } add_filter( 'mime_types', 'wpse294198_mime_types' ); You can use command line tool file (linux|macOS) to see the mime type, e.g. file --mime-type -b somefile.xls


4

This question had me scratching my head. Yeah, how come WordPress doesn't support this natively? And then I found out. You asked how to upload SVG in WordPress 4.9.8 (the current version at the time of writing). You mention that you "tried uploading after installing different plugins". You don't say which plugins, nor whether they relate to SVG. As I ...


3

Argh..easy to fix: instead of include( ABSPATH . 'wp-admin/includes/image.php' ); use require_once( ABSPATH . 'wp-admin/includes/image.php' ); Hope this helps someone!


3

To be honest I'm a bit puzzled that all what you have tried so far is copying & pasting an answer that doesn't solve your problem. On the other hand, you described your problem clearly. Below I outline a solution for you, take a look at the additional comments and information sources, if you really want to understand what's happening. Code: // The ...


3

There must be something identifiable about your form data, such as your input names. Check for one or more of those and process accordingly. function custom_upload_dir($args) { if (isset($_POST['something'])) { $args['path'] = $args['basedir'] . "/mypath" . $args['subdir']; $args['url'] = $args['baseurl'] . "/mypath" . $args['subdir'];...


3

I've managed to do it with the filter image_make_intermediate_size. Probably all the path/filename.extension dismembering and remaking could be optimized or made in a single stroke fashion, but alas, I'll let that to the reader: // The filter runs when resizing an image to make a thumbnail or intermediate size. add_filter( 'image_make_intermediate_size', '...


3

Now I have an answer for my own question.I solved this issue by using this code.I add this code just for sharing and helping other as it works for me. <input type="file" name="my_file_upload" id="my_file_upload_id" class="bg_checkbox" > function register_team_show_case_setting() { //register our settings register_setting('...


3

If I'm not mistaken, then it should be possible. You want to use media_handle_upload(), which calls wp_handle_upload() to handle the file upload. wp_handle_upload() on itself won't be helpful, especially because it is pretty much just a wrapper for _wp_handle_upload(). It's not the end yet, because inside _wp_handle_upload() the function wp_upload_dir() is ...


2

EDIT: With some help of a friend I came up with a solution. For everyone interested: Use a custom post-type, in my case comment_post. Then upload the images like this: $new_post = array( 'post_title' => $title, 'post_content' => $comment, 'post_status' => 'pending',// Choose: publish, preview, future, draft, etc. 'post_type' => '...


2

Uploading files in ajax is a bit tricky because it is not possible to upload files using the browser's XMLHttpRequest object so you need to use some kind of Ajax upload plugin. Also wp_handle_upload() is not what your using in your code its media_handle_sideload() wp_handle_upload() - should be used for file uploads (input file field) media_handle_sideload(...


2

u can use this: function wpse128538_resize($url, $width, $height = null, $crop = null, $single = true) { //validate inputs if (!$url OR !$width) return false; //define upload path & dir $upload_info = wp_upload_dir(); $upload_dir = $upload_info['basedir']; $upload_url = $upload_info['baseurl']; //check if $img_url is local ...


2

this is how I've done it preg_match('/\.[^\.]+$/i',$file['file']['name'],$ext); $name = md5(time().$user_id.rand( 5, 97)); $name_one = $name.$ext[0]; $name_blur = $name.'_blur'.$ext[0]; $file['file']['name'] = $name_one; $upload = wp_upload_dir(); $uploaded_file = wp_handle_upload( $file['file'], array( 'action'=> 'bp_upload_profile_bg' ) );...


2

The $post_id argument for media_handle_upload is used to set the column post_parent for attachment post. Posts without any parent have 0 as post parent, not null. So, use 0 as argument and it will work. If you read Codex page linked above it states: ... If you don't want this media attached to a specific post, you can pass 0


2

I have tested this with an image I created myself with Photoshop where I inserted the word "Süss" in every thinkable IPTC field. I uploaded it to my WordPress 4.6 installation, which has no image handling plugins installed. The uploading went smoothly, the right thumbnails were created in the uploads directory and the caption field was loaded correctly from ...


2

You can use media_sideload_image() which will take a link to a file and upload it to the media library. You do need to pass it some kind of $post_id @Sumit points out in the comments you could pass NULL into the $post_id field. media_sideload_image( $file_url, $post_id, $image_desc, $return ); If you need the uploaded image ID, take a look ...


2

I mentioned in a comment how it's important to debug your code. Here's why: The images are added first. In the image adding section, you're running this line of code: $_FILES = array("moreimages" => $image); Then when you get to your routine that adds the files, you start with this: $files = $_FILES['morefiles']; Can you see what's wrong here? At ...


2

Yes, you should have quite reasonable options to customize upload, there are plenty of existing solutions around. As for specifics I am not sure on "database" as image storage option. They typical solutions that I've heard of are: dedicated location behind CDN (or some variation of); dedicated image storage service, such as Amazon S3.


2

//add SVG to allowed file uploads function add_file_types_to_uploads($file_types){ $new_filetypes = array(); $new_filetypes['svg'] = 'image/svg+xml'; $file_types = array_merge($file_types, $new_filetypes ); return $file_types; } add_action('upload_mimes', 'add_file_types_to_uploads');


1

You can try adding the input file field once and using then using jQuery to add more fields when clicking add more. Here are the steps Register your setting using register_setting function Create Menu using add_menu_page. This also requires a calback function Add the thickbox and media-upload script using wp_enqueue_script and wp_enqueue_style Use the ...


1

I've modified your code a little to check that the post type is a page, that the current user can edit that page, verify the nonce and verify the inserting attachment functions. The resulting code is working. It seems that don't checking the post type could be the reason of the issue. Also, you don't need to manually include 'wp-admin/includes/image.php': ...


Only top voted, non community-wiki answers of a minimum length are eligible