Dropzone.js and wordpress plugin

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.

Dropzone:

<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" />
</form>

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);
                var_dump($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);
        }
    }
}



}

1 Answer
1

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">
</form>

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() {
    status_header(200);
    // load your processing.php file
    die();
}

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

Leave a Comment