Change upload URL by mime type

It turns out the value that points to the PDFs’ locations on disk is stored in the wp_postmeta table, under the meta_value field. For example a media item located at site.com/app/uploads/2019/06/whatever.pdf would have a meta_value value of 2019/06/whatever.pdf. To change the location of all PDFs I first found the IDs of all PDFs (from the … Read more

wp_handle_upload specified file failed upload test

Please add below code in your theme functions.php file : add_filter( ‘wp_check_filetype_and_ext’, ‘file_and_ext_allow_for_user’, 10, 4 ); function file_and_ext_allow_for_user( $types, $file, $filename, $mimes ) { if( false !== strpos( $filename, ‘.doc’ ) ) { $types[‘ext’] = ‘doc’; $types[‘type’] = ‘application/msword’; } else if( false !== strpos( $filename, ‘.pdf’ ) ) { $types[‘ext’] = ‘pdf’; $types[‘type’] = … Read more

Get post attachment with post id

yes use this code here you can get attachment id if(isset($_FILES[‘myimage’]) && ($_FILES[‘myimage’][‘size’] > 0)) { // Get the type of the uploaded file. This is returned as “type/extension” $arr_file_type = wp_check_filetype(basename($_FILES[‘myimage’][‘name’])); $uploaded_file_type = $arr_file_type[‘type’]; // Set an array containing a list of acceptable formats $allowed_file_types = array(‘image/jpg’, ‘image/jpeg’, ‘image/gif’, ‘image/png’); if (in_array($uploaded_file_type, $allowed_file_types)) { … Read more

How to allow .xls file uploads?

Try the same function but different $mime_types key (‘xla|xls|xlt|xlw’): <?php function my_myme_types( $mime_types ){ // New allowed mime types. $mime_types[‘xla|xls|xlt|xlw’] = ‘application/vnd.ms-excel’; return $mime_types; } add_filter( ‘upload_mimes’, ‘my_myme_types’, 1, 1 ); To get MIME type array keys and values used by WordPress: <?php $mimes = wp_get_mime_types(); echo ‘<pre>’; print_r( $data ); echo ‘</pre>’;