I’m creating a custom post type, “gallery”, in which the admin should be able to upload images (these images would be attached to the post).
The thing is that the “editor” meta box is disabled for this post type. And I need a way to add the image upload pop-up box to it, just like posts have. How can I do that?
or maybe it’s better to create my own uploader? if so, how could I attach the uploaded images to the post (gallery) that’s being created?
how do attachments work? are they custom post types too?
At the top of
wp-admin/edit-form-advanced.php
I see the following code that seems related to the media uploader:You’ll need to add these yourself.
add_thickbox()
enqueues both a script and a style, so make sure you hook intoprint_styles
, asprint_scripts
will be too late to also print a style.Now we need to add the upload buttons. I see
the_editor()
, the function that displays the editor, has a parameter$media_buttons
, and if we set to totrue
it basically executesdo_action('media_buttons')
. This in turn callsmedia_buttons()
, which calls_media_button()
for each media type (image, video, audio, …). So we do this ourselves!Attachments are indeed custom posts of type
attachment
, with theirpost_parent
set to the post they’re attached to. Images have two meta fields:_wp_attached_file
contains the filename,_wp_attachment_metadata
contains an array with image EXIF data and pointers to different sizes of the same image. You can create these yourself, usingwp_insert_attachment()
, but I believe you still have to handle the upload yourself then.Above answer is very helpful answer but sad thing is _media_button() function is deprecated from wp 3.5
So i have changed above code like this :
Replace this line of code
echo _media_button(__('Add an Image'), 'images/media-button-image.gif?ver=20100531', 'image');
With these lines :
and finally I used code to add media button to my gallery post type is like this