Handling front-end file uploads, considering safety and ease of use

I think the easiest way, since you’re already using the wp_editor function is going to be to just include the media buttons in the WP_Editor instance – this way you’ll have the native functions, including the “Insert into post” button, built in for free.

How you do this obviously depends on the plugin you’re trying to work with. However, this should get you started. Include code like this in a page template to display the editor, and you’ll get an editor on your page. Including this in a form and processing the results is another step not detailed here.

// Define the global variable $post_id - this is used by the media uploader
// to attach uploads to a specific post (so that the uploader can see uploads
// attached to this post and not others)
global $post_id;
$post_id = $post->ID; // should be the ID of the new post created

// Now filter the list of tabs available in the media editor.
// Remove everything but the "From Computer" option.

add_filter( 'media_upload_tabs', 'wpse42068_remove_additional_tabs' );

function wpse42068_remove_additional_tabs( $_default_tabs ) {
    return array( 'type' => __('From Computer') );
}

// Now just include the WP_Editor. See
// http://codex.wordpress.org/Function_Reference/wp_editor
// for settings available
wp_editor( '', 'posteditor', array( 'media_buttons' => true ) );

Defining the post ID is probably the critical part, and how you do this is will depend on the logic of your functionality. I would suggest:

  • Creating an auto-draft on first visiting this page, and saving the post ID returned in the global $post_id variable.
  • Then saving the created post with that same ID when the form is submitted.

Leave a Comment