Created
September 18, 2013 20:56
-
-
Save ryelle/6615561 to your computer and use it in GitHub Desktop.
Pop up a media modal to upload/select an XML file. This assumes specific HTML output in attach_xml_box (the metabox callback). Use the function in enqueue.php to load custom-media.js & the files required for media on the hompage edit screen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Uploading files | |
| var file_frame; | |
| ( function( $ ) { | |
| $(document).ready( function(){ | |
| var $container = $( document.getElementById( 'attach-xml' ) ); | |
| $container.on( 'click', '.upload', function( event ){ | |
| event.preventDefault(); | |
| var $this = $( this ); | |
| // If the media frame already exists, reopen it. | |
| if ( file_frame ) { | |
| file_frame.open(); | |
| return; | |
| } | |
| // Create the media frame. | |
| file_frame = wp.media.frames.file_frame = wp.media({ | |
| title: $this.data( 'title' ), | |
| button: { | |
| text: $this.data( 'select' ), | |
| }, | |
| // Specify only text files, since we want only XML. | |
| library: { | |
| type: 'text' | |
| }, | |
| multiple: false | |
| }); | |
| // When an image is selected, run a callback. | |
| file_frame.on( 'select', function() { | |
| // We set multiple to false so only get one image from the uploader | |
| attachment = file_frame.state().get('selection').first().toJSON(); | |
| // Do something with attachment.id and/or attachment.url here | |
| if ( 'text' == attachment.type ){ | |
| $container.find( '[name="xml_post_id"]' ).val( attachment.id ); | |
| $container.find( '.display' ).html( attachment.title ); | |
| $container.find( '.remove' ).show(); | |
| $container.find( '.upload' ).hide(); | |
| } | |
| }); | |
| // Finally, open the modal | |
| file_frame.open(); | |
| }); | |
| $container.on( 'click', '.remove', function(){ | |
| $container.find( '[name="xml_post_id"]' ).val( '' ); | |
| $container.find( '.display' ).html( '<i>none selected</i>' ); | |
| $container.find( '.upload' ).show(); | |
| $container.find( '.remove' ).hide(); | |
| }); | |
| } ); | |
| } )( jQuery ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //[... inside a class] | |
| public function add_media( $screen ) { | |
| if ( 'post.php' == $screen && 'homepage' == get_post_type() ) { | |
| wp_enqueue_media(); | |
| wp_enqueue_script( 'wired-admin', get_template_directory_uri() . '/custom-media.js', array( 'jquery' ) ); | |
| } | |
| } | |
| add_action( 'admin_enqueue_scripts', array( $this, 'add_media' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment