I'm working with a frontend upload form that uses blueimp jQuery-File-Uploader to do all the heavy lifting ... but, after attempting to add the necessary WordPress functionality to blueimp I started running into some errors that I am unable to figure out.

Update / My code

HTML (the frontend form):

<form id="fileupload" action="" method="POST" enctype="multipart/form-data">

    <span class="btn btn-success fileinput-button">
        <span>Add files...</span>
        <!-- The file input field used as target for the file upload widget -->
        <input type="file" name="files[]" multiple />

    <div id="progress" class="progress">
        <div class="progress-bar progress-bar-success"></div>

JS (I'm using the same js that is used in the Basic Plus demo file) Just changing the url variable to the location of the UploadHandler.php file

PHP (relative parts of my upload script ... I'm using the default UploadHandler script and trying to customize it for WordPress)

// Required WordPress files and hooks
require_once( $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php" );
require_once( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/media.php" );
require_once( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php" );
require_once( $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php" );

$upload_dir = wp_upload_dir();

global $current_user;
$logged_in_user = $current_user->ID;

// Some default blueimp code

// First few lines of the __construct function
global $upload_dir;
$this->options = array(
    'script_url' => $this->get_full_url(),
    'upload_dir' => $upload_dir['path'] . '/',
    'upload_url' => $upload_dir['url'] . '/',

// A lot more default script code

// The handle_file_upload function with WordPress customizations
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
    $index = null, $content_range = null) {
    global $logged_in_user;
    $file = new stdClass();
    $file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error,
    $index, $content_range);
    $file->size = $this->fix_integer_overflow(intval($size));
    $file->type = $type;
    if ($this->validate($uploaded_file, $file, $error, $index)) {
        $this->handle_form_data($file, $index);
        $upload_dir = $this->get_upload_path();
        if (!is_dir($upload_dir)) {
            mkdir($upload_dir, $this->options['mkdir_mode'], true);
        $file_path = $this->get_upload_path($file->name);
        $append_file = $content_range && is_file($file_path) &&
            $file->size > $this->get_file_size($file_path);

        if ($uploaded_file && is_uploaded_file($uploaded_file)) {
            // multipart/formdata uploads (POST method uploads)
            if ($append_file) {
                    fopen($uploaded_file, 'r'),
            } else {
                move_uploaded_file($uploaded_file, $file_path);
        } else {
            // Non-multipart uploads (PUT method support)
                fopen('php://input', 'r'),
                $append_file ? FILE_APPEND : 0
        $file_size = $this->get_file_size($file_path, $append_file);
        if ($file_size === $file->size) {
            $file->url = $this->get_download_url($file->name);
            if ($this->is_valid_image_file($file_path)) {
                $this->handle_image_file($file_path, $file);
        } else {
            $file->size = $file_size;
            if (!$content_range && $this->options['discard_aborted_uploads']) {
                $file->error = $this->get_error_message('abort');

    $attachment = array(
        'post_mime_type'    => $file->type,
        'post_title'        => preg_replace( '/\.[^.]+$/', '', basename( $file_path )),
        'post_content'      => '',
        'post_author'       => $logged_in_user,
        'post_status'       => 'inherit',
        'post_type'         => 'attachment',
        'guid'              => $this->options['upload_url'] . $name

    $attachment_id = wp_insert_attachment( $attachment, $file_path );

    // Generate the attachment data
    $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path );

    // Update the attachment metadata
    wp_update_attachment_metadata( $attachment_id, $attachment_data );

    return $file;

Updated Issue:

At first this seems to work ... but, when you check the wp_posts database table nothing has been added. Also, when looking in the media library it only shows the uploaded image (none of the other images, which are in the database) and the following errors appear in the error_log:

PHP Warning: strpos() expects parameter 1 to be string, object given in /Applications/MAMP/htdocs/wordpress/wp-includes/post.php on line 188 PHP Warning: preg_match() expects parameter 2 to be string, object given in /Applications/MAMP/htdocs/wordpress/wp-includes/post.php on line 188 PHP Catchable fatal error: Object of class stdClass could not be converted to string in /Applications/MAMP/htdocs/wordpress/wp-includes/post.php on line 189

I understand the fact that the variable that I'm calling is an object, not a string, but I'm unsure what to put in it's place.

Any help is appreciated have tried everything I can think of. Really need to get this up and running ASAP! Thanks


2 Answers 2


After doing tests and working with a friend I was able to find a solution:

Just change how the function get_full_url grabs the url:

// Go to 'protected function get_full_url()'
// Line 200 (approx. if you've made the same changes stated above) in UploadHandler.php 
// change this line, should be the last one in the function
substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));

// After editing this is what you should end with
substr($_SERVER['DOCUMENT_ROOT'],0, strrpos($_SERVER['DOCUMENT_ROOT'], '/'));

This is what worked for me (along with the customized code from the question), allowing me to use the demo uploader in a custom WordPress page. Creating a frontend upload form! Hope this helps someone.


I recently modified a Wordpress page to include the file upload plugin. On a standalone test page the control - also modified from the basicplus sample - worked fine. However, in Wordpress uploads failed. I removed the form wrapper and that fixed it.

  • Thanks for the effort to answer my question ... but your response was a bit hard to understand. Feb 7, 2014 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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