Hello
I created an Image upload with WordPress Meta-box, but it works only with “JPG” extension
By this code
function add_custom_meta_boxes() {
// Define the custom attachment for posts
add_meta_box(
'wp_custom_attachment',
'Product Images',
'wp_custom_attachment',
'post',
'normal'
);
} // end add_custom_meta_boxes
add_action('add_meta_boxes', 'add_custom_meta_boxes');
function wp_custom_attachment() {
wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');
$html = '<p class="description">';
$html .= 'Upload your Image here.';
$html .= '</p>';
$html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';
echo $html;
} // end wp_custom_attachment
function save_custom_meta_data($id) {
/* --- security verification --- */
if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) {
return $id;
} // end if
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $id;
} // end if
if('page' == $_POST['post_type']) {
if(!current_user_can('edit_page', $id)) {
return $id;
} // end if
} else {
if(!current_user_can('edit_page', $id)) {
return $id;
} // end if
} // end if
/* - end security verification - */
// Make sure the file array isn't empty
if(!empty($_FILES['wp_custom_attachment']['name'])) {
// Setup the array of supported file types. In this case, it's just JPG.
$supported_types = array('image/jpg', 'image/jpeg', 'image/pjpeg');
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if(in_array($uploaded_type, $supported_types)) {
// Use the WordPress API to upload the file
$upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));
if(isset($upload['error']) && $upload['error'] != 0) {
wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
} else {
add_post_meta($id, 'wp_custom_attachment', $upload);
update_post_meta($id, 'wp_custom_attachment', $upload);
} // end if/else
} else {
wp_die("The file type that you've uploaded is not a JPG.");
} // end if/else
} // end if
} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');
function update_edit_form() {
echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');
and it`s work good.
My question now is “How do I create a multiple image uploads and give it more supported_types?”
Thank you
There is a quick and good way/practice to achieve this.
http://metabox.io/meta-box/
I know you did not ask for a plugin but for multiple image uploads I stumbled upon this free plugin that fits all needs: ACF Photo Gallery Field. Simple add this gallery field to the post type you want and you can easily attach as many images as you want. Also fetching the images can be done with a few lines of code.
Here’s is my working solution, Im making the use of basic HTML multiple upload form, you just need to give different treatment to
$_FILES
, here you can find the documentation.With this, you will generate different metadata for each image uploaded, now the next thing you need to do is to properly set up your metabox callback, the input field should be something like this:
Then you will need to properly display the files, I will let that up to you, but with this implementation, the files are saved properly as metadata fields on the order.
i find that code in site and did some modification on it