Attaching media to custom posts without editor

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?

Read More

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?

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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>';
        }
    }