Last active
December 7, 2016 13:30
-
-
Save miya0001/5024989 to your computer and use it in GitHub Desktop.
WordPress 3.5の新メディアアップローダーの使い方
This file contains 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(document).ready(function($){ | |
var custom_uploader; | |
$('#demo-media').click(function(e) { | |
e.preventDefault(); | |
if (custom_uploader) { | |
custom_uploader.open(); | |
return; | |
} | |
custom_uploader = wp.media({ | |
title: 'Choose Image', | |
// 以下のコメントアウトを解除すると画像のみに限定される。 | |
/* | |
library: { | |
type: 'image' | |
}, | |
*/ | |
button: { | |
text: 'Choose Image' | |
}, | |
multiple: true // falseにすると画像を1つしか選択できなくなる | |
}); | |
custom_uploader.on('select', function() { | |
var images = custom_uploader.state().get('selection'); | |
images.each(function(file){ | |
$('#demo-images').append('<img src="'+file.toJSON().url+'" />'); | |
}); | |
}); | |
custom_uploader.open(); | |
}); | |
}); |
This file contains 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 | |
/* | |
Plugin Name: Media Uploader DEMO | |
Author: Takayuki Miyauchi | |
*/ | |
new my_plugin(); | |
class my_plugin { | |
function __construct() | |
{ | |
add_action('admin_menu', array(&$this, 'admin_menu')); | |
} | |
public function admin_menu() | |
{ | |
$hook = add_menu_page( | |
'Media DEMO', | |
'Media DEMO', | |
'update_core', | |
'media-uploader-demo', | |
array(&$this, 'admin_page') | |
); | |
add_action('admin_print_scripts-'.$hook, array(&$this, 'admin_scripts')); | |
} | |
public function admin_page() | |
{ | |
?> | |
<style type="text/css"> | |
#demo-images img | |
{ | |
max-width: 100px; | |
max-height: 100px; | |
margin: 10px; | |
border: 1px solid #cccccc; | |
} | |
</style> | |
<div class="wrap"> | |
<h2>メディアアップローダーのデモ</h2> | |
<button id="demo-media">画像を選択</button> | |
<div id="demo-images"></div> | |
</div><!-- .wrap --> | |
<?php | |
} | |
public function admin_scripts() | |
{ | |
wp_enqueue_media(); // メディアアップローダー用のスクリプトをロードする | |
// カスタムメディアアップローダー用のJavaScript | |
wp_enqueue_script( | |
'my-media-uploader', | |
plugins_url("media-uploader.js", __FILE__), | |
array('jquery'), | |
filemtime(dirname(__FILE__).'/media-uploader.js'), | |
false | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
詳しくは以下のページ
http://firegoby.jp/archives/4031