|

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.

Similar Posts

  • |

    Announcing Responsive Design with WordPress

    If you’ve been milling around the site lately, or follow me on Facebook, Twitter, Google+, LinkedIn, or Dribbble, you’ll know I’ve been working on a book. Last week I officially launched the site for @NEPABlogCon and I’m happy today to officially announce my second book, Responsive Design with WordPress. Responsive Design with WordPress shows readers Responsive…

  • Links Round Up for 7/25/10

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! How To Design And Style Your WordPress Plugin Admin Panel: This helped me immensely this week. Great for any WordPress…

  • How Do I Learn Web Development?

    A couple of years ago, I was on the How to Hold a Pencil podcast (episode here) and we discussed a number of things, including the steps I took to learn web development (over 14 years ago…whoa). I’m often asked, as I was on the show, what I recommend for people starting today. Here’s my…

  • NEPAWP: WordPress is Installed… Now What?

    Join us for the next NEPA WordPress Meetup, March 26th (this Tuesday) at 7pm at The Vintage Theater. My friend and colleague Phil Erb will be giving a talk called WordPress is Installed…Now What?. From the website: Phil Erb will be presenting WordPress is Installed… Now What? – covering the basics of site settings, themes, and plugins….