0
votes

I am writing a widget plugin for Wordpress and I want to insert an input tag:<input type="file"> to browse for a pattern in my widget plugin admin panel; but I have problem to save its value.

It seems that <input type="file"> doesn't work in widget plugins. Is it correct? Are there any other ways to browse a file from the admin panel?

1
Your message is not really clear. Could you edit it? - JonasVautherin
@JonesV, thanks for your comment! it's important for me to know the reason of the language corrections. So, what about my main question? - Truth
I don't understand the "browse for a pattern" part. Don't you simply want to allow the user to select a file from her disk? - JonasVautherin
@JonesV, something like this! I want to let the user to select an image such as a pattern from his webserver. to get address of the image for its display. - Truth

1 Answers

0
votes

I made it using combination of arbitrary file input and hidden input for image path. Here're the steps:

  1. create hidden field that will be saved and dummy file input
  2. bind change event to file input (I'm using jQuery)
  3. on change collect FormData object and send it via ajax to ajaxurl
  4. add action('wp_ajax_hook', 'my_callback_func') in functions.php, where hook = your action
  5. move uploaded file(s) to upload dir
  6. return file path from my_callback_func
  7. fill hidden field with file path on ajax success
  8. display uploaded image for user, so that he konws upload was successful (optional)
  9. PROFIT!!! Now you can save you form

I will not post code snippets here, cause they are rather trivial for most developers