Last active
          October 28, 2015 03:42 
        
      - 
      
- 
        Save s-hiroshi/6d3b1a9b7a90db1f3032 to your computer and use it in GitHub Desktop. 
  
    
      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
    
  
  
    
  | add_action( 'admin_enqueue_scripts', 'example_register_script', 10 ); | |
| function example_register_script() { | |
| wp_enqueue_media(); /* WordPressのメディアアップローダーを利用可能にします。 */ | |
| wp_enqueue_script( | |
| 'example-uploader', | |
| /path/to/example-uploader.js', | |
| array( 'jquery' ), | |
| '1.0.0', | |
| false | |
| ); | |
| } | 
  
    
      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
    
  
  
    
  | <div class="example-uploader"> | |
| <h2>画像</h2> | |
| <form method="post" action=""> | |
| <div class="image-block"> | |
| <div class="image-elem"> | |
| <input type="text" class="image-url" name="image-url" size="50"> | |
| </div> | |
| <div class="image-elem"> | |
| <button class="image-select">画像追加</button> | |
| </div> | |
| <div class="image-elem"> | |
| <div class="image-preview"></div> | |
| </div> | |
| <div class="image-elem"> | |
| <button class="image-remove">画像削除</button> | |
| </div> | |
| </div> | |
| </form> | |
| </div> | 
  
    
      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
    
  
  
    
  | jQuery( function ( $ ) { | |
| $( ".image-select" ).on( "click", function () { | |
| var media_loader, parent; | |
| parent = $( this ).parents( ".image-block" ); | |
| if ( media_loader ) { | |
| media_loader.open(); | |
| return; | |
| } | |
| /** @var object wp recieved form wordpress */ | |
| media_loader = wp.media( { | |
| title: "Insert", | |
| button: { | |
| text: "Insert" | |
| }, | |
| multiple: false | |
| } ); | |
| media_loader.on( "select", function () { | |
| var image, preview; | |
| image = media_loader.state().get( "selection" ); | |
| preview = $( ".image-preview", parent ); | |
| image.each( function ( file ) { | |
| $( ".image-url", parent ).val( file.toJSON().url ); | |
| var img = $( "img", preview ); | |
| if ( img.length === 0 ) { | |
| preview.append( '<img src="' + file.toJSON().url + '">' ); | |
| } else { | |
| img.attr( "src", file.toJSON().url ); | |
| } | |
| preview.css( "display", "block" ); | |
| } ); | |
| } ); | |
| media_loader.open(); | |
| return false; | |
| } ); | |
| // Remove images | |
| $( ".image-remove" ).on( 'click', function ( e ) { | |
| var parent, preview; | |
| parent = $( this ).parents( ".image-block" ); | |
| $( ".image-url", parent ).val( "" ); | |
| preview = $( ".image-preview", parent ); | |
| if ( $( "img", preview ).length > 0 ) { | |
| $( "img", preview ).remove(); | |
| } | |
| return false; | |
| } ); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment