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

About Michal Zuber

Biker and rollerblader. Owner and developer at http://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/
This entry was posted in php, wordpress and tagged . Bookmark the permalink.

One Response to Use WordPress media upload modal

  1. Antoine says:

    It’s work ! Thank you so much ! I search a solution for to much time :’)

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