• I am learning WordPress Plugin Development. In this regard I have to upload image through my plugin. How to upload image in WordPress ? I am looking for WordPress way to upload image. I used media_handle_upload() function for this.

    I would like to upload image in my desired location/folder and rename the image. How can I do that ?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the file input field on your form for uploading files. When the form is submitted, the file entered in the field is uploaded to PHP’s temp folder. Use the appropriate index in $_FILES to call media_handle_upload(), which would normally move files from the temp folder to somewhere in wp-content/uploads/. You can override the move portion of the function with the ‘pre_move_uploaded_file’ filter. Move the file yourself to where you want it, using the data passed to your callback. Return true to tell WP that moving has been handled and for it to skip that part.
    https://core.trac.www.ads-software.com/browser/tags/5.5/src/wp-admin/includes/file.php#L880

    Also hook ‘wp_handle_upload’ (applied at line 948 in above linked source code) to update the returned data to reflect the location you moved the file to.

Viewing 1 replies (of 1 total)
  • The topic ‘Image upload in WordPress plugin’ is closed to new replies.