public
Last active

WordPressプラグイン内でメディアアップローダーを使用してファイル名を選択しテキストフィールドに挿入する

  • Download Gist
select_media.html
Liquid
1 2 3 4 5 6 7
<h3>My Picture (1)</h3>
<input type="text" id="my_media_1" name="my_media_1" value="" />
<a class="media-upload" href="JavaScript:void(0);" rel="my_media_1">Select File</a>
 
<h3>My Picture (2)</h3>
<input type="text" id="my_media_2" name="my_media_2" value="" />
<a class="media-upload" href="JavaScript:void(0);" rel="my_media_2">Select File</a>
select_media.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
jQuery('document').ready(function(){
jQuery('.media-upload').each(function(){
var rel = jQuery(this).attr("rel");
jQuery(this).click(function(){
window.send_to_editor = function(html) {
imgurl = jQuery('img', html).attr('src');
jQuery('#'+rel).val(imgurl);
tb_remove();
}
formfield = jQuery('#'+rel).attr('name');
tb_show(null, 'media-upload.php?post_id=0&type=image&TB_iframe=true');
return false;
});
});
});
select_media.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
 
add_action('admin_print_scripts-'.$hook_suffix, 'my_admin_scripts');
add_action('admin_print_styles-'.$hook_suffix, 'my_admin_styles');
 
function my_admin_styles() {
wp_enqueue_style('thickbox');
}
 
function my_admin_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('jquery');
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.