title: How to open Wordpress Media Upload in two places in plugin
body: <p>I'm writing a plugin and have need of the media uploader in two different functions; add program, and edit program. Both these functions present a form for the site admin to fill in. One form for adding a new program, and the other for editing an existing one (facilitated by an id variable passed in the URL).</p>
  
  <p>The code I got from this page: <a href="http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/" rel="nofollow">http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/</a> is working perfectly in the add program admin page. However, the same code is not working on the edit program page. I've checked the source of both pages, and the appropriate scripts and styles are enqueued, the javascript is present in the body, just before the form code, and the upload field and button codes are correct. Everything tis as it should be on both pages, but it only works on one of them.</p>
  
  <p>Being quite a novice at javascript and jQuery, it's quite possible I'm missing something simple. Any assistance would be appreciated.</p>
  
  <p>Hi cale b, the only error shown is "tb_show is not defined" which, of course, isn't good. It LOOKS like it's defined in the code, though.</p>
  
  <p>Here is the code for enqueuing the scripts and styles:</p>
  
  <pre><code>function bhcprograms_manager_admin_scripts() {
      wp_enqueue_script('jquery');
      wp_enqueue_script('media-upload');
      wp_enqueue_script('thickbox');
  }
  
  function bhcprograms_manager_admin_styles() {
      wp_enqueue_style('thickbox');
  }
  
  add_action('admin_print_scripts', 'bhcprograms_manager_admin_scripts');
  add_action('admin_print_styles', 'bhcprograms_manager_admin_styles');
  </code></pre>
  
  <p>The javascript and form code for the one that works:</p>
  
  <pre><code>echo "&lt;script language=\"JavaScript\"&gt;\n";
  echo "jQuery(document).ready(function() {\n";
  echo "jQuery('#bhc_image_button').click(function() {\n";
  echo "formfield = jQuery('#bhc_image').attr('name');\n";
  echo "tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');\n";
  echo "return false;\n";
  echo "});\n";
  
  echo "window.send_to_editor = function(html) {\n";
  echo "imgurl = jQuery('img',html).attr('src');\n";
  echo "jQuery('#bhc_image').val(imgurl);\n";
  echo "tb_remove();\n";
  echo "}\n";
  
  echo "});\n";
  echo "&lt;/script&gt;\n";
  
  echo "&lt;form method=\"post\" action=\"\" name=\"new_program\"&gt;\n";
  echo "&lt;h4&gt;&amp;ast; indicates required field&lt;/h4&gt;\n";
  echo "&lt;h4&gt;Program Title &amp;ast;&lt;/h4&gt;\n";
  echo "&lt;input type=\"text\" size=\"40\" name=\"bhc_title\" value=\"" . str_replace('"', '&amp;quot;', $bhc_title) . "\" maxlength=\"255\" /&gt;\n";
  echo "&lt;h4&gt;Program Subtitle&lt;/h4&gt;&lt;strong&gt;If no subtitle, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
  echo "&lt;input type=\"text\" size=\"40\" name=\"bhc_subtitle\" value=\"" . str_replace('"', '&amp;quot;', $bhc_subtitle) . "\" maxlength=\"255\" /&gt;\n";
  echo "&lt;h4&gt;Description &amp;ast;&lt;/h4&gt;\n";
  echo "&lt;textarea name=\"bhc_description\" rows=\"4\" cols=\"50\" type=\"textarea\"&gt;" . $bhc_description . "&lt;/textarea&gt;\n";
  echo "&lt;h4&gt;Image Upload&lt;/h4&gt;&lt;strong&gt;Upload an image to go with this program description. If no image wanted, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
  echo "&lt;input id=\"bhc_image\" type=\"text\" size=\"40\" name=\"bhc_image\" value=\"" . $bhc_image . "\" /&gt;\n";
  echo "&lt;input id=\"bhc_image_button\" type=\"button\" value=\"Upload Image\" /&gt;\n";
  echo "&lt;h4&gt;What to Bring&lt;/h4&gt;&lt;strong&gt;If N/A, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
  echo "&lt;textarea name=\"bhc_bring\" rows=\"4\" cols=\"50\" type=\"textarea\"&gt;" . $bhc_bring . "&lt;/textarea&gt;\n";
  echo "&lt;h4&gt;What is Provided&lt;/h4&gt;&lt;strong&gt;If N/A, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
  echo "&lt;textarea name=\"bhc_provided\" rows=\"4\" cols=\"50\" type=\"textarea\"&gt;" . $bhc_provided . "&lt;/textarea&gt;\n";
  echo "&lt;br /&gt;&lt;br /&gt;\n";
  echo "&lt;h4&gt;Start program as active?&lt;/h4&gt;\n";
  echo "&lt;input type=\"radio\" name=\"bhc_active\" value=\"1\"" . ($bhc_active==1?' checked':'') . "&gt; Yes&lt;br /&gt;&lt;input type=\"radio\" name=\"bhc_active\" value=\"0\"" . ($bhc_active==1?'':' checked') . "&gt; No&lt;br /&gt;\n";
  echo "&lt;br /&gt;&lt;br /&gt;\n";
  echo "&lt;input type=\"submit\" name=\"submit\" class=\"button-primary\" value=\"Add Program\" /&gt;\n";
  echo "&lt;/form&gt;\n";
  </code></pre>
  
  <p>and the javascript and form codes for the one that doesn't work:</p>
  
  <pre><code>echo "&lt;script language=\"JavaScript\"&gt;\n";
  echo "jQuery(document).ready(function() {\n";
  echo "jQuery('#bhc_image_button').click(function() {\n";
  echo "formfield = jQuery('#bhc_image').attr('name');\n";
  echo "tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');\n";
  echo "return false;\n";
  echo "});\n";
  echo "window.send_to_editor = function(html) {\n";
  echo "imgurl = jQuery('img',html).attr('src');\n";
  echo "jQuery('#bhc_image').val(imgurl);\n";
  echo "tb_remove();\n";
  echo "}\n";
  echo "});\n";
  echo "&lt;/script&gt;\n";
      /* Show the form with the details */
      echo "&lt;form method=\"post\" action=\"\" name=\"edit_program\"&gt;\n";
      echo "&lt;strong&gt;&amp;ast; indicates required field&lt;/strong&gt;\n";
      /* Find out if there are events scheduled for this program ... */
      $bhc_program_event = $mydb-&gt;get_results("SELECT bhc_event_id FROM $table_events WHERE bhc_program_id = $bhc_id");
      /* Loop through and display programs*/
      echo "&lt;h4&gt;Program Title &amp;ast;&lt;/h4&gt;\n";
      if ($bhc_program_event) {
          echo "&lt;strong&gt;" . $bhc_title . "&lt;/strong&gt;\n";
          echo "&lt;input type=\"hidden\" name=\"bhc_title\" value=\"" . str_replace('"', '&amp;quot;', $bhc_title) . "\" /&gt;\n";
      } else {
          echo "&lt;input type=\"text\" size=\"40\" name=\"bhc_title\"  value=\"" . str_replace('"', '&amp;quot;', $bhc_title) . "\" maxlength=\"255\" /&gt;\n";
      }
      echo "&lt;h4&gt;Program Subtitle&lt;/h4&gt;\n";
      if ($bhc_program_event) {
          echo "&lt;strong&gt;" . $bhc_subtitle . "&lt;/strong&gt;\n";
          echo "&lt;input type=\"hidden\" name=\"bhc_subtitle\" value=\"" . str_replace('"', '&amp;quot;', $bhc_subtitle) . "\" /&gt;\n";
      } else {
          echo "&lt;strong&gt;If no subtitle, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
          echo "&lt;input type=\"text\" size=\"40\" name=\"bhc_subtitle\" value=\"" . str_replace('"', '&amp;quot;', $bhc_subtitle) . "\" maxlength=\"255\" /&gt;\n";
      }
      echo "&lt;h4&gt;Description &amp;ast;&lt;/h4&gt;\n";
      echo "&lt;textarea name=\"bhc_description\" rows=\"4\" cols=\"50\" type=\"textarea\"&gt;" . $bhc_description . "&lt;/textarea&gt;\n";
      echo "&lt;h4&gt;Image Upload&lt;/h4&gt;&lt;strong&gt;Upload an image to go with this program description. If no image wanted, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
      echo "&lt;input id=\"bhc_image\" type=\"text\" size=\"40\" name=\"bhc_image\" value=\"" . $bhc_image . "\" /&gt;\n";
      echo "&lt;input id=\"bhc_image_button\" type=\"button\" value=\"Upload Image\" /&gt;\n";
      echo "&lt;h4&gt;What to Bring&lt;/h4&gt;&lt;strong&gt;If N/A, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
      echo "&lt;textarea name=\"bhc_bring\" rows=\"4\" cols=\"50\" type=\"textarea\"&gt;" . $bhc_bring . "&lt;/textarea&gt;\n";
      echo "&lt;h4&gt;What is Provided&lt;/h4&gt;&lt;strong&gt;If N/A, leave it blank&lt;/strong&gt;&lt;br /&gt;\n";
      echo "&lt;textarea name=\"bhc_provided\" rows=\"4\" cols=\"50\" type=\"textarea\"&gt;" . $bhc_provided . "&lt;/textarea&gt;\n";
      /* Exclude active option if registrations exist */
      $nowtime = date("U");
      $bhc_reg_check = $mydb-&gt;get_row("SELECT bhc_event_id FROM $table_events WHERE bhc_program_id = $bhc_id AND bhc_current_peeps &gt; 0 LIMIT 1");
      if ($bhc_reg_check) {
          echo "&lt;input type=\"hidden\" name=\"bhc_active\" value=\"1\"&gt;\n";
      } else {
          echo "&lt;h4&gt;Set program as active?&lt;/h4&gt;\n";
          echo "&lt;input type=\"radio\" name=\"bhc_active\" value=\"1\"" . ($bhc_active==1?' checked':'') . "&gt; Yes&lt;br /&gt;&lt;input type=\"radio\" name=\"bhc_active\" value=\"0\"" . ($bhc_active==1?'':' checked') . "&gt; No&lt;br /&gt;\n";
      }
      echo "&lt;br /&gt;&lt;br /&gt;\n";
      echo "&lt;input type=\"hidden\" name=\"bhc_id\" value=\"" . $bhc_id . "\" /&gt;\n";
      echo "&lt;input type=\"submit\" name=\"submit\" class=\"button-primary\" value=\"Edit Program\" /&gt;\n";
      echo "&lt;/form&gt;\n";
      exit;
  </code></pre>