2929 "thumbnail " => array (
3030 "name " => "_thumbnail " ,
3131 "std " => "" ,
32- "title " => "添加图片地址,调用指定缩略图 " ,
33- "type " =>"text " ),
32+ "title " => "添加图标地址,调用自定义图标 " ,
33+ "size " => "" ,
34+ 'button_text ' => '添加图标 ' ,
35+ "type " =>"upload " ),
3436);
3537
3638// 面板内容
@@ -73,7 +75,13 @@ function new_meta_sites_boxes() {
7375 else $ checked = '' ;
7476 echo '<br /><label><input type="checkbox" name=" ' . $ meta_box ['name ' ] . '" value="true" ' . $ checked . ' /> ' ;
7577 echo '' . $ meta_box ['title ' ] . '</label><br /> ' ;
76- break ;
78+ break ;
79+ case 'upload ' :
80+ $ button_text = (isset ($ meta_box ['button_text ' ])) ? $ meta_box ['button_text ' ] : 'Upload ' ;
81+ echo '<h4> ' . $ meta_box ['title ' ] . '</h4> ' ;
82+ echo '<input class="damiwp_url_input" style="width: 95%;margin-bottom: 10px;" type="text" id=" ' .$ meta_box ['name ' ].'_input" size=" ' .$ meta_box ['size ' ].'" value=" ' .$ meta_box ['std ' ].'" name=" ' .$ meta_box ['name ' ].'"/><a href="#" id=" ' .$ meta_box ['name ' ].'" class="dami_upload_button button"> ' .$ button_text .'</a> ' ;
83+ add_script_and_styles ();
84+ break ;
7785 }
7886 }
7987}
@@ -103,3 +111,28 @@ function save_sites_postdata($post_id) {
103111add_action ('admin_menu ' , 'create_meta_sites_box ' );
104112add_action ('save_post ' , 'save_sites_postdata ' );
105113
114+ function add_script_and_styles () {
115+ echo "<script>
116+ jQuery(document).ready(function(){
117+ var dami_upload_frame;
118+ var value_id;
119+ jQuery('.dami_upload_button').live('click',function(event){
120+ value_id =jQuery( this ).attr('id');
121+ event.preventDefault();
122+ if( dami_upload_frame ){
123+ dami_upload_frame.open();
124+ return;
125+ }
126+ dami_upload_frame = wp.media({
127+ title: '上传图片',
128+ button: {
129+ text: '确定',
130+ },
131+ multiple: false
132+ });
133+ dami_upload_frame.on('select',function(){
134+ attachment = dami_upload_frame.state().get('selection').first().toJSON();
135+ //jQuery('#'+value_id+'_input').val(attachment.url).trigger('change');
136+ jQuery('input[name='+value_id+']').val(attachment.url).trigger('change');});dami_upload_frame.open();});});
137+ </script> " ;
138+ }
0 commit comments