|

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

  • My Travel Schedule for Fall 2019

    Every year there seems to be a season where I travel more than usual, and this year it starts today! Here’s a list of conferences (or cities) I’ll be visiting now through the end of the year. If you’re going to be around at the same time, let me know!

  • Good Books for Web Development

    I’m teaching a couple of web development classes this semester and have been receiving a lot of questions as to what books would serve as good resources. Here’s a short list, starting with the ones I’ve chosen for my classes. HTML and CSS: Design and Build Websites: This is possibly the best book I’ve used for…

  • New way to comment!

    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!Dude to the massive amounts of spam that I just got today (over ~50 messages in a few hours), I installed…

  • |

    Quick Tip: Use Post or Page Slug as CSS Class in WordPress

    I don’t know why, but for some reason, I thought WordPress already included the slug of a post or page in either the function body_class() or post_class(). Doing some work over this weekend I realized that’s not the case, but luckily, it’s very easy to do. The functions body_class() and post_class() serve as a way…

  • Never Assume When Teaching WordPress

    Over the weekend I gave a Lightning Talk at WordCamp US called Never Assume When Teaching WordPress. The response was overwhelmingly great! While I don’t normally do this, since it was a shorter talk I’ve decided to post the script I wrote for it. You can check it out after the jump.

  • |

    Join Me for CigarCamp at WordCamp US!

    With WordCamp US quickly approaching, I thought it would be fun to have an informal CigarCamp US while everyone is in town. Here are the details: Location: Ashton Cigar Bar (map) Date: Friday, December 4th Time: 9:00pm – 11:00pm EST Head on over to the site and sign up! There will be cigars, scotch, and WordPress talk. And…