0

I'm developing a wordpress page to submit a post and its images attachment. I don't use input type="file" instead use jQuery wordpress media library.

  frame.on("select", function () {

  var attachments = frame
    .state()
    .get("selection")
    .map(function (attachment) {
      attachment.toJSON();
      return attachment;
    });

  for (i = 0; i < attachments.length; ++i) {
    //sample function 1: add image preview
    imgContainer.after(
      '<div class="jrl-image-preview"><img src="' +
        attachments[i].attributes.url +
        '" ></div>'
    );

    //sample function 2: add hidden input for each image
    imgContainer.after(
      '<input id="jrl-image-input' +
        attachments[i].id +
        '" type="hidden" name="jrl_attachment_id_array[]"  value="' +
        attachments[i].id +
        '">'
    );
  }

But I don't know how to save the image files into wordpress DB and upload folder. I tried using $_FILES to get uploaded files but its empty array.

Thanks in advance

6
  • edit your question to show us the javascript code you use to send files.
    – mmm
    Commented Aug 9 at 11:26
  • That's all javascript code I have to open media library and preview the images. There's no code to send files. I am developing a form in front end to create a post with its attachment images. Commented Aug 9 at 12:44
  • if you use media library, it uploads the file and then you juste have to save the image id. look this : wordpress.stackexchange.com/questions/388224/…
    – mmm
    Commented Aug 9 at 13:34
  • Thanks but I don't see a script to save the images. As you can see in the script above it can preview selected multiple images (with its hidden value) from media library but when I click on Submit button there is no value in $_FILES. I once using <input type="file" ... and there is value in $_FILES but I prefer use wp media library rather than the html tag. Commented Aug 9 at 13:50
  • as I said, it's the media library which do the upload operations. then it returns the identifier of the file and you just have to manage this identifier in your php code.
    – mmm
    Commented Aug 9 at 15:11

0

Your Answer

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