-2

My question is regarding a WordPress site. Using JavaScript or jQuery, on the entire front-end of the website, I would like to add the attribute accept="image/*" to all of the <input type="file">. Could someone please help me achieve this?

JOKKER
  • 502
  • 6
  • 21
  • 1
    FYI: **Java** and **JavaScript** are completely different languages. And **jQuery** is a JavaScript library. – Manas Khandelwal Dec 15 '21 at 17:02
  • While this is possible, doing it through JS is not very reliable. Remember to always validate and virus scan any content uploaded to your server. – Rory McCrossan Dec 15 '21 at 17:04
  • @RoryMcCrossan, _"Remember to always validate and virus scan any content uploaded to your server."_ - How would I go about doing that? Any good tutorial you could point me to? – JOKKER Dec 15 '21 at 17:11

2 Answers2

3

Using jquery in wordpress

The following code uses jquery approach to solve your problem:

jQuery(document).ready($ => {

  $('input[type=file]').each(function(){
    $(this).attr('accept', 'image/*');
  });

})
Mokhless
  • 686
  • 2
  • 6
  • 22
1

Quite trivial:

document.querySelectorAll("[type=file]")
 .forEach(file => file.setAttribute("accept","image/*"))

You will still need to check the type of file on the server too

For example

Wordpress plugin development - File upload: How to allow specified file types?

mplungjan
  • 169,008
  • 28
  • 173
  • 236