I'm currently using Dropzone.js within a personal plugin and I'm able to create the dropzone, however when I pass in plugins_url as the action I'd like to get the files and add them to the wordpress media using the usual insert_attachment function found on the wordpress documentation.


<form id="udev-dropzone" method = "post" action="<?php echo plugins_url('/unrealdevs-portfolio/library/components/PortfolioUploader.php'); ?>" class="dropzone" enctype="multipart/form-data">
    <input type="hidden" name="submitted" id="submitted" value="1" />

My statement in PortfolioUploader.php to check for files (I know I need to add a nonce):

if ( $_FILES ) {
$files = $_FILES;
$user = $_POST['user_id'];
foreach ($files['name'] as $key => $value) {
    if ($files['name'][$key]) {
        $file = array(
            'name'     => $files['name'][$key],
            'type'     => $files['type'][$key],
            'tmp_name' => $files['tmp_name'][$key],
            'error'    => $files['error'][$key],
            'size'     => $files['size'][$key]

            $_FILES = array("upload_attachment" => $file);
            update_udevdb($user, $file); //Dismiss this line.
            foreach ($_FILES as $file => $array) {
                $newupload = insert_attachment($file);

insert_attachment function:

function insert_attachment($file_handler) {

  // check to make sure its a successful upload
  if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  require_once(ABSPATH . "wp-admin" . '/includes/media.php');

  $attach_id = media_handle_upload( $file_handler, 0 );

Can anyone assist me? :)

EDIT: Here is the code that was used to correctly insert_attachment

function udev_before_uploader() { 
if ( !empty($_FILES) ) {
    $files = $_FILES;
    foreach($files as $file) {
        $newfile = array (
            'name' => $file['name'],
            'type' => $file['type'],
            'tmp_name' => $file['tmp_name'],
            'error' => $file['error'],
            'size' => $file['size']

        $_FILES = array('upload'=>$newfile);
        foreach($_FILES as $file => $array) {
            $newupload = insert_attachment($file);

  • Thanks for adding the insert_attachment answer! Been pulling my hair out here :) – jetlej Oct 28 '14 at 3:17

I assume the issue is that your form targets an external file, where WordPress isn't properly loaded to access the API.

WordPress has a handler built in that you can use to process POST requests, via the admin_post action:

Example form with hidden action field:

<form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="post">
  <input type="hidden" name="action" value="add_foobar">
  <input type="hidden" name="data" value="somedata">
  <input type="submit" value="Submit">

Example action, mapped to action passed from form:

add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
//this next action version allows users not logged in to submit requests
add_action( 'admin_post_nopriv_add_foobar', 'prefix_admin_add_foobar' );

function prefix_admin_add_foobar() {
    // load your processing.php file

WordPress will be loaded in this context, giving you access to the API in your form processing code.

  • 1
    Thank you very much! I've successfully fixed my code. I had created an action that is called before the dropzone and then called the post there. However your method of using admin-post seems much more plausible. The other issue was getting it to correctly pass into my insert_attachment function. Which was fixed with the code in my edited question. Thank you very much Milo, learning more every day :) – Sythen Jun 4 '14 at 6:22
  • Thanks both of you, I just have completed mine using both of yours discussion. One point I want to add that admin-post.php accepts one action on which it binds its do_aciton() hook. I passed mine simply using admin_url('admin-post.php?action=my_post_action'); Though I don't its good or bad. Once again thanks for the great help. – maksbd19 Apr 1 '15 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.