Upload to the Media Library From Your Plugin

There are a variety of reasons you may want or need to include an upload form inside your theme or plugin. One thing that bugs me, though, is when the image (or whatever) that I’ve uploaded isn’t available in the Media Library. I don’t want to have to reupload an image just so I can reuse it in a blog post.

The solution is pretty easy – let WordPress do the dirty work for you.

<?php
if ( isset( $_FILES[ 'some-file' ][ 'name' ] ) && !empty( $_FILES[ 'some-file' ][ 'name' ] ) ) {
    $upl_id = media_handle_upload( 'some-file', 0 );
    if (is_wp_error( $upl_id )) {
        echo '<div class="error"><p>'. $upl_id->errors['upload_error']['0'] .'</p></div>';
    }
    else {
        echo '<div class="updated"><p>"'. get_the_title( $upl_id ) .'" was uploaded!';
        echo wp_attachment_is_image( $upl_id ) ? '<br />' . wp_get_attachment_image( $upl_id ) : '';
        echo '</p></div>';
    }
}
?>
<form action="" method="post" enctype="multipart/form-data">
<p><input type="file" class="button" name="some-file" id="some-file" />
<input type="submit" class="button-primary" value="Upload"/>
<input type="hidden" name="submitted-some-file" /></p>
</form>

This is a very simple example, of course. Go play with it

More Sorting Options for the Media Library

If you work with a lot of PDFs in WordPress, you may find the lack of a “view PDFs” link annoying, especially considering that you can separate image and audio attachments easily.

Fortunately, you can fix that.

add_filter('post_mime_types', 'add_post_mime_type');
function add_post_mime_type( $post_mime_types ) {
	//$post_mime_types['application'] = array(__('Doc'), __('Manage Doc'), _n_noop('Doc <span class="count">(%s)</span>', 'Doc <span class="count">(%s)</span>'));
	$post_mime_types['application/pdf'] = array(__('PDF'), __('Manage PDF'), _n_noop('PDF <span class="count">(%s)</span>', 'PDF <span class="count">(%s)</span>'));
	$post_mime_types['application/msword'] = array(__('DOC'), __('Manage DOC'), _n_noop('DOC <span class="count">(%s)</span>', 'DOC <span class="count">(%s)</span>'));
	return $post_mime_types;
}

Remove the Media Library Tab

I’ll leave the ‘why’ to you, but here’s the ‘how’

add_filter( 'media_upload_tabs', 'no_media_library_tab' );
function no_media_library_tab( $tabs ) {
    unset($tabs['library']);
    return $tabs;
}

Okay, one possible reason would be that you want to restrict users from accessing media that’s been attached to another post.

Remove Library Tab from Post-Side Media Pop-Up

I’m working on a plugin right now where I’m having to hide or limit a lot of features depending on the current user’s capabilities. Among those limitations is media uploading and editing access. I’m using this snippet to hide the Media Library tab from the Add an Image pop-up on the edit post page.

add_filter('media_upload_tabs','no_library_tab');
function no_library_tab($tabs) {
    if (!current_user_can('promote_users')) { 
        unset($tabs['library']);
    }
    return $tabs;
}