|

Adding the Media Uploader in WordPress

…without including the Editor. In the Admin. That was a really long title, so I hope you don’t feel mislead! I was recently working on a project that required a Custom Post Type without the editor, but needed the Media Uploader.

Note: This is not a full-blown tutorial. The purpose of this post is to help those troubleshoot the fact that the media uploader is not working, given the conditions above. 

Here is how I define the media custom meta box (this is only part of a bigger array of arguments):

array(
    'title' => __( 'Upload File', 'jlc' ),
    'type' => array( 'custom-post-type' ),
    'id' => 'upload-file',
    'items' => array(
        array(
            'type' => 'media',
            'name' => '_upload_file',
            'label' => __( 'Upload File', 'jlc' ),
            'label_position' => 'before',
        ),
    ),
),

In the post type definition, here’s what the ‘supports’ argument looks like:

'supports' => array(
    'title',
    'page-attributes',
    )

Note that the editor is not listed. If it were, the media uploader scripts would be automatically added. Instead, you get an error that wp.media is undefined. Luckily, there’s an easy fix for this. Simply add this line in where you custom post type is defined:

add_action( 'admin_init', 'wp_enqueue_media' );

This says that when you are on the admin, add the required media scripts. That way, even if the editor isn’t loaded, the media uploader will be.

Leave a Reply

Your email address will not be published. Required fields are marked *