I am using CBM2 plugin within my own custom plugins. I need create my own file uploader so that the file will be stored at a different location than the metapost table within word press. Thus I am creating my own custom field type and using Javascript to call a php file, in order to do the upload.

The problem is the XMLHttpRequest open, says the file does not exist and the log comes back with a "missing page" and yet the files does exist in the plugin folder.

I have tried hard coding the pathway to the file. I also needed to change the 3rd parameter to false (synchronous) in order to get any response at all.

 xhttp.open("POST", "handle_file_upload.php", false); 

Does anyone have any ideas on how to create this "post" so that I can upload the file to the server without it going into the WordPress system?

The code:

 function cmb2_render_callback_photo_upload($field, $escaped_value, 
 $object_id, $object_type, $field_type_object)
    function UploadFile()
      var xhttp = new XMLHttpRequest();
      xhttp.open("POST", "handle_file_upload.php", true); 
      xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xhttp.setRequestHeader("Connection", "close"); 
      xhttp.onload = function () {

      var Access     = document.getElementById("myFile");
      var UploadFile = Access.files[0];
      var fd         = new FormData();
      fd.append("myFile", UploadFile);

     alert('before send');

    <input type="file" name="myFile" id="myFile" accept="image/*" onchange="UploadFile()"/>

 add_action( 'cmb2_render_photo_upload', 'cmb2_render_callback_photo_upload', 10, 5 );
  • Sending requests directly to a custom PHP file in a theme/plugin or in the root directory is bad practice and can be very tricky to get right while introducing security issues. Ifyou need to make a request to a website with JS in WP, you should use the REST API to handle that request, not a dedicated PHP file you ping directly. Also keep in mind that CMB2 is a 3rd party plugin, 3rd party plugins aren't on topic here – Tom J Nowell May 22 '19 at 20:08
  • Don't have a choice. Previous investment trap and no time. The CBM2 plugin tool requires it since it is based on JavaScript. I don't require help with CBM2, I require help with how to enable a "false post submit" in order to retrieve and upload a file outside of the WordPress tools. Thus the code. Besides, I am limiting the upload to a png/jpg file and it is not access by wordpress. No security issues. But it MUST fit into the CBM2 environment as it becomes part of that tool kit. – Debbie Kurth May 22 '19 at 20:26
  • ...besides, the code is executed behind a login. Not public – Debbie Kurth May 22 '19 at 20:35

Your Answer

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

Browse other questions tagged or ask your own question.