skip to Main Content

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?

2

Answers


  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?

    Login or Signup to reply.
  2. 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/*');
      });
    
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search