Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

At the top of wp-admin/edit-form-advanced.php I see the following code that seems related to the media uploader:

if ( post_type_supports($post_type, 'editor') || post_type_supports($post_type, 'thumbnail') ) {
    add_thickbox();
    wp_enqueue_script('media-upload');
}

You'll need to add these yourself. add_thickbox() enqueues both a script and a style, so make sure you hook into print_styles, as print_scripts will be too late to also print a style.

add_action('admin_print_styles-post-new.php', 'wpa4016_add_media_upload_scripts');
add_action('admin_print_styles-post.php', 'wpa4016_add_media_upload_scripts');
function wpa4016_add_media_upload_scripts()
{
    if ($GLOBALS['post_type'] == 'wpa4016') {
        add_thickbox();
        wp_enqueue_script('media-upload');
    }
}

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 to true it basically executes do_action('media_buttons'). This in turn calls media_buttons(), which calls _media_button() for each media type (image, video, audio, ...). So we do this ourselves!

add_action('edit_form_advanced', 'wpa4016_edit_form_advanced');
function wpa4016_edit_form_advanced()
{
    if ($GLOBALS['post_type'] == 'wpa4016') {
        echo _media_button(__('Add an Image'), 'images/media-button-image.gif?ver=20100531', 'image');
    }
}

Attachments are indeed custom posts of type attachment, with their post_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, using wp_insert_attachment(), but I believe you still have to handle the upload yourself then.

share|improve this answer
    
tx, i tried this and it works, kind of. the problem is that functionality is limited. "insert into post" button missing, gallery tab acting weird. So I guess it's better to make my own upload function. I assume wp_handle_upload() will take care of the uploaded files, but how can I attach these files to the current post? –  Alex Nov 13 '10 at 20:24
    
@Alex: If the attached file has its post_parent field set to the post ID of your "gallery" post, you can get all images via a get_children() query. That's how "normal" attachments work. –  Jan Fabry Nov 13 '10 at 20:27
    
tx, wp insert attachment() seems to do what I want. Any suggestions for a good multifile uploader? –  Alex Nov 13 '10 at 20:39
    
Nice answer. I'll look into this too - seems like a lot of people have been wanting this kind of functionality... –  goldenapples Nov 13 '10 at 22:01
    
Alex asked a followup question about the upload handler, now they are linked. –  Jan Fabry Nov 15 '10 at 9:41
add comment

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 :

$img = '<span class="wp-media-buttons-icon"></span> ';
       echo '<a href="#" class="button insert-media add_media" data-editor="' . esc_attr( $editor_id ) . '" title="' . esc_attr__( 'Add Media' ) . '">' . $img . __( 'Add Media' ) . '</a>';

and finally I used code to add media button to my gallery post type is like this

add_action('admin_print_styles-post-new.php', 'gallery_add_media_upload_scripts');
add_action('admin_print_styles-post.php', 'gallery_add_media_upload_scripts');
function gallery_add_media_upload_scripts()
{
    if ($GLOBALS['post_type'] == 'gallery') {
        add_thickbox();
        wp_enqueue_script('media-upload');
    }
}

add_action('edit_form_advanced', 'gallery_edit_form_advanced');
function gallery_edit_form_advanced()
{
    if ($GLOBALS['post_type'] == 'gallery') {
        $img = '<span class="wp-media-buttons-icon"></span> ';
       echo '<a href="#" class="button insert-media add_media" data-editor="' . esc_attr( $editor_id ) . '" title="' . esc_attr__( 'Add Media' ) . '">' . $img . __( 'Add Media' ) . '</a>';
    }
}
share|improve this answer
1  
add_thickbox() will likely also get deprecated. –  kaiser Jan 3 '13 at 13:06
    
I dont know about add_thickbox() but above code worked well for theme... –  Makarand Mane Jan 4 '13 at 15:44
    
This doesn't work if your custom post type doesn't use an editor. –  Cam Tullos Feb 21 '13 at 2:52
    
@CamTullos You are right. I added this button because my custom post type not supported with editor –  Makarand Mane Feb 22 '13 at 11:55
1  
@Bohdan Hdal It should work. I have no problem at all in 3.5.2 –  Makarand Mane Jul 22 '13 at 9:10
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.