Use WordPress media upload modal

In a project I needed selecting uploaded attachments and the media upload modal would fit flawlessly.

Before calling wp_enqueue_script() I had to add wp_enqueue_media().

The HTML part required a <button> with the following attributes data-editor="editor-id" class="button insert-media"

In JavaScript part I used the attachment value:

$( document ).on( 'click', '.insert-media', function( event ) {

  wp.media.editor.send.attachment = function( props, attachment ) {
      $( '#output' ).val( attachment.url );
  }
});

Resources that helped me were http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/ and http://waterstreetgm.org/passing-php-variables-to-javascript-with-wp_localize_script/

Advertisements

Author: Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at https://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/

1 thought on “Use WordPress media upload modal”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s