0

I wrote some custom code which I've stuck into the media upload panel using a filter. It does a couple of things. First, it changes the text for the "submit" button for users who add things through the uploader. Second, it makes it so that the contents of the File URL field automatically enters the image upload input for a form I created when an image for it is uploaded through the media uploader. (that's the JQuery). Finally, there's CSS to hide elements of the media uploader that users don't need to see.

When this function is in the functions.php file, it causes the media library to stall in loading up the images. It just hangs on the spinning grey ball. But it also works.

How can I integrate this code into the site in a way that doesn't keep the media uploader from working?

add_filter('attachment_fields_to_edit', 'my_plugin_action_button', 20, 2);

function my_plugin_action_button($form_fields, $post) {

  $send = "<input type='submit' class='button' name='send[$post->ID]' value='" . esc_attr__( 'Use Photo' ) . "' />";

  $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send</td></tr>\n");
  ?>

  <script>
    jQuery('.savesend input[type=submit]').click(function(){  
      var url = jQuery(this).parents('.describe').find('.urlfile').data('link-url');
      var field = jQuery(this).parents('.describe').find('.urlfield');
      field.val(url);
    });
  </script>
  <style>
    #media-head-125, #imgedit-response-125, .savebutton.ml-submit, .image-size, .align, .post_content, .post_excerpt, .image_alt, .post_title.form-required, .media-types.media-types-required-info, .url {
      display: none !important;
    }
  </style>

  <?php
  return $form_fields;
}
2
  • What exactly is the purpose of this? Do you want to use the Media Uploader for a custom Meta Box? Sep 1, 2017 at 6:56
  • Precisely. Users will get backend access to upload images to use as business card (it's a social networking site). Sep 18, 2017 at 4:23

2 Answers 2

0

Check if media html elements have previous classes, probably You removed some class from media window (button i guess) and some js functions can not be triggered like hide().

0

I think you have to change only two places (see arrows):

....
$form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend mySender123'>$send</td></tr>\n");
?>
                                                                                                 ⇧⇧⇧ 
<script>
jQuery('.mySender123 input[type=submit]').click..................
            ⇧⇧⇧ 
......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.