1

I am currently encountering a very strange problem.

With the help of two existing wordpress functions (wp_handle_upload and wp_mail) and a form that uses some custom ajax functionality to process my data, I am trying to send that data to my email address. This includes a few text inputs and a file.

Most of my endeavour seems to be going fine (data is send from my form, it is received in my php file etc.) however wp_handle_upload never returns anything when fed with my $_files data. Debugging supported my assumption as my file is correctly send/attached to my mail whenever I use my $_files data directly (without feeding it to wp_handle_upload first).

The problem here of course is that I would be juggling with temporary submit data and would also never receive the file in its correct state (as in: its name is a temporary one that does not include its file ending etc.). So this is obviously no solution to my problem does however provide me with the information that something is going wrong whenever I use wp_handle_upload.

My guess is there might be some safety check within that function which my form data does not pass so it will always return NULL. With the current implementation I will always receive a mail without any attachments.

I have been looking at a lot of already posted questions and adapted my solution accordingly but sadly to no avail. Thank you very much in advance and have a nice day.

The code below was adapted slightly (changed names, removed if statements etc.) to not clutter my post too much. Everything should still be functional however.

JS File (included on page load):

jQuery(function($){

$(document).ready(function(){
    register.init();
});

var register= {

    init : function(){
        this.createEvents();
    },

    createEvents : function(){

        var t = this;          
        $( "form" ).submit(function() {
            t.submitForm($(this));
        });
    },

    submitForm: function(clicked){

        var data = new FormData();

        data.append('action', 'register_form');             
        data.append('firstname', $('#firstname').val());
        data.append('lastname', $('#lastname').val());
        data.append('email', $('#email').val());
        data.append('attachmentFile', $('#attachmentFile')[0].files[0] );       

        jQuery.ajax({
        url : registerFormAjax.ajaxurl,
        type: 'POST',
        data : data,
        cache: false,
        processData: false, 
        contentType: false 
    });          
    } 

};

});

PHP File (custom plugin):

add_action( 'wp_ajax_register_form', 'register_form' );
add_action( 'wp_ajax_nopriv_register_form', 'register_form' ); 
function register_form()
{   
    $kv_errors= array();
    if('POST' == $_SERVER['REQUEST_METHOD'])  {
        $fields = array('firstname', 'lastname', 'email');      
        foreach ($fields as $field) {
            if (isset($_POST[$field])) $posted[$field] = 
               stripslashes(trim($_POST[$field])); else $posted[$field] = '';
        }
        if ( ! function_exists( 'wp_handle_upload' ) ) {
            require_once( ABSPATH . 'wp-admin/includes/file.php' );
        }

        if(isset($_FILES['attachmentFile'])) 
        {       
            $uploadedfile = $_FILES['attachmentFile'];
            $upload_overrides = array( 'test_form' => false );                  
            $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );


            $attachment = $movefile['file'];
            $headers = 'From: '.$posted['firstname']. ' ' . $posted['lastname'].' <'.$posted['email'].'>' . "\r\n";
            wp_mail('test@test.com', 'Subject' , "Test", $headers, $attachment);
            unlink( $movefile['file'] );

        }
    }

wp_die();
}

EDIT:

Alright. I seem to have found my actual problem. The solution will hopefully soon follow. For some reason my overrides won't work properly. Meaning even deactivating form checks like so

$upload_overrides = array( 'test_form' => false );

will not skip the following test done within the wordpress function (wp_handle_upload)

// A correct form post will pass this test.
if ( $test_form && ( ! isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) ) {
    return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) );
}
0

Alright so I feel rather stupid now, especially because I have found the solution to my own problem right after posting my question here, but also because I would have found it much earlier had I properly debugged my page (implemented a way to look at return error codes).

Anyways...

As stated in my EDIT above: my form tests were still being executed despite me overriding them. I still don't really know why the posted solution is not functional as it is taken straight from the documentation but I have found a "workaround".

Creating the override array inside my function call actually works like I would expect it to:

$movefile = wp_handle_upload( $uploadedfile, array( 'test_form' => false ) );

Sorry guys. Happy debugging!

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.