title: Correct way to use wp_enqueue_script() inside the functions.php file
body: <p>Im using wp_enqueue_script() in my functions.php file to give users on the front end the ability to upload images with dropzone.js. It's making my side extremely slow and I get this error:</p>
  
  <pre><code>wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks.
  </code></pre>
  
  <p>Here's the problematic code it works great and does exactly what I want but slows my site to a crawl. What am I doing wrong here?</p>
  
  <pre><code>wp_enqueue_script('dropzone','wp-content/themes/storefront/assets/js/dropzone.js', array('jquery'));
  wp_enqueue_script('my-script','wp-content/themes/storefront/footer.php', array('jquery','dropzone'));
   $drop_param = array(
    'upload'=&gt;admin_url( 'admin-ajax.php?action=handle_dropped_media' ),
    'delete'=&gt;admin_url( 'admin-ajax.php?action=handle_delete_media' ),
  );
  wp_localize_script('my-script','dropParam', $drop_param);
  
  
  
  add_action( 'wp_ajax_handle_dropped_media', 'BMP_handle_dropped_media' );
  add_action( 'wp_ajax_nopriv_handle_dropped_media', 'BMP_handle_dropped_media' );
  
  function BMP_handle_dropped_media() {
     // status_header(200);
      $upload_dir = wp_upload_dir();
      $upload_path = $upload_dir['path'] . DIRECTORY_SEPARATOR;
      $num_files = count($_FILES['file']['tmp_name']);
      $post_id = get_the_ID();
  
      $newupload = 0;
  
      if ( !empty($_FILES) ) {
          $files = $_FILES;
          foreach($files as $file) {
              $newfile = array (
                      'name' =&gt; $file['name'],
                      'type' =&gt; $file['type'],
                      'tmp_name' =&gt; $file['tmp_name'],
                      'error' =&gt; $file['error'],
                      'size' =&gt; $file['size']
              );
  
              $_FILES = array('upload'=&gt;$newfile);
              foreach($_FILES as $file =&gt; $array) {
                  $newupload = my_handle_attachment( $file, $post_id );
              }
          }
      }
  
      echo $newupload;    
      die();
  }
  
  add_action( 'wp_ajax_handle_delete_media', 'BMP_handle_delete_media' );
  add_action( 'wp_ajax_nopriv_handle_delete_media', 'BMP_handle_delete_media' );
  function BMP_handle_delete_media(){
  
      if( isset($_REQUEST['media_id']) ){
  
          $post_id = absint( $_REQUEST['media_id'] );
  
          $status = wp_delete_attachment($post_id, true);
  
          if( $status )
              echo json_encode(array('status' =&gt; 'OK'));
          else
              echo json_encode(array('status' =&gt; 'FAILED'));
      }
  
      die();
  }
  </code></pre>
  
  <p>Thanks for any help.
  Heres where I pulled the code from originally.
  <a href="https://stackoverflow.com/questions/29411257/how-to-integrate-dropzonejs-with-wordpress-media-handler-in-frontend/41295520">How to integrate Dropzonejs with wordpress media handler in frontend?
  </a> </p>