wordpress进阶教程(九):在自定义面板中添加图片上传功能


通过自定义字段给文章添加一些附属图片什么的,应用很广,一般技术不到位的,会使用自定义字段填写图片url来实现,那样虽然实现了,但是非常不方便,我们要的是不经能直接填写图片url,而且能直接上传,并且要能够实时预览。其实理论很简单,跟我们后台教程中添加图片上传功能基本一样,可参考:wordpress主题后台制作(八):图片上传。

OK,我们还是沿用上一篇教程中的代码,并且直接在上一篇教程中添加图片上传功能:

首先打开我们的metabox.php文件,在配置数组中添加一项:

  1. "uploads" => array(   
  2.         "name" => "_meta_uploader",   
  3.         "std" => '',      
  4.         "title" => "图片上传",   
  5.         "type"=>"uploader"),  
  6. case 'uploader':   
  7.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  8.                 //图片预览框   
  9.                 if($meta_box['std'] != ''){   
  10.                 echo '<span id="'.$meta_box['name'].'_value_img"><img src='.$meta_box['std'].' alt="" /></span>';}   
  11.                 echo '<input class="ashu_upload_input" type="text" size="40" value="'.$meta_box['std'].'" name="'.$meta_box['name'].'_value"/>';   
  12.                 echo '<input type="button" value="上传" class="ashu_bottom"/>';   
  13.                 echo '<br/>';   
  14.                 break;  
  15. jQuery(document).ready(function() {   
  16.     jQuery('input.ashu_bottom').click(function() {      
  17.         custom_editor = true;          
  18.          targetfield = jQuery(this).prev('input');      
  19.          tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');   
  20.          window.original_send_to_editor = window.send_to_editor;   
  21.          window.send_to_editor = function(html) {   
  22.             if (custom_editor) {       
  23.             imgurl = jQuery('img',html).attr('src');   
  24.             jQuery(targetfield).val(imgurl).focus();   
  25.             custom_editor = false;   
  26.             tb_remove();   
  27.             }else{   
  28.                 window.original_send_to_editor(html);   
  29.             }   
  30.         }           
  31.         return false;      
  32.     });      
  33.        
  34.        
  35.           
  36.     //图片实时预览ashu_upload_input为图片url文本框的class属性      
  37.     jQuery('input.ashu_upload_input').each(function()      
  38.     {      
  39.         jQuery(this).bind('change focus blur', function()      
  40.         {         
  41.             //获取改文本框的name属性后面      
  42.             $select = '#' + jQuery(this).attr('name') + '_img';      
  43.             $value = jQuery(this).val();      
  44.             $image = '<img src ="'+$value+'" />';      
  45.                               
  46.             var $image = jQuery($select).html('').append($image).find('img');      
  47.                   
  48.             //set timeout because of safari      
  49.             window.setTimeout(function()      
  50.             {      
  51.                 if(parseInt($image.attr('width')) < 20)      
  52.                 {         
  53.                     jQuery($select).html('');      
  54.                 }      
  55.             },500);      
  56.         });      
  57.     });      
  58.        
  59. });  
  60. wp_enqueue_script('kriesi_custom_fields_js', get_template_directory_uri(). '/js/metaup.js');  
  61. <?php   
  62. $new_meta_boxes =    
  63. array(   
  64.     "title" => array(   
  65.         "name" => "_meta_title",   
  66.         "std" => "",   
  67.         "title" => "标题",   
  68.         "type"=>"title"),      
  69.      
  70.     "keywords" => array(   
  71.         "name" => "_meta_keywords",   
  72.         "std" => "",      
  73.         "title" => "关键字",   
  74.         "type"=>"text"),   
  75.            
  76.     "description" => array(   
  77.         "name" => "_meta_description",   
  78.         "std" => "",      
  79.         "title" => "描述%

 第二步:在函数new_meta_boxes中的switch语句中添加一项即可,其实我们直接将后台教程中提到的上传代码复制过来即可用:

  1. "uploads" => array(   
  2.         "name" => "_meta_uploader",   
  3.         "std" => '',      
  4.         "title" => "图片上传",   
  5.         "type"=>"uploader"),  
  6. case 'uploader':   
  7.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  8.                 //图片预览框   
  9.                 if($meta_box['std'] != ''){   
  10.                 echo '<span id="'.$meta_box['name'].'_value_img"><img src='.$meta_box['std'].' alt="" /></span>';}   
  11.                 echo '<input class="ashu_upload_input" type="text" size="40" value="'.$meta_box['std'].'" name="'.$meta_box['name'].'_value"/>';   
  12.                 echo '<input type="button" value="上传" class="ashu_bottom"/>';   
  13.                 echo '<br/>';   
  14.                 break;  
  15. jQuery(document).ready(function() {   
  16.     jQuery('input.ashu_bottom').click(function() {      
  17.         custom_editor = true;          
  18.          targetfield = jQuery(this).prev('input');      
  19.          tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');   
  20.          window.original_send_to_editor = window.send_to_editor;   
  21.          window.send_to_editor = function(html) {   
  22.             if (custom_editor) {       
  23.             imgurl = jQuery('img',html).attr('src');   
  24.             jQuery(targetfield).val(imgurl).focus();   
  25.             custom_editor = false;   
  26.             tb_remove();   
  27.             }else{   
  28.                 window.original_send_to_editor(html);   
  29.             }   
  30.         }           
  31.         return false;      
  32.     });      
  33.        
  34.        
  35.           
  36.     //图片实时预览ashu_upload_input为图片url文本框的class属性      
  37.     jQuery('input.ashu_upload_input').each(function()      
  38.     {      
  39.         jQuery(this).bind('change focus blur', function()      
  40.         {         
  41.             //获取改文本框的name属性后面      
  42.             $select = '#' + jQuery(this).attr('name') + '_img';      
  43.             $value = jQuery(this).val();      
  44.             $image = '<img src ="'+$value+'" />';      
  45.                               
  46.             var $image = jQuery($select).html('').append($image).find('img');      
  47.                   
  48.             //set timeout because of safari      
  49.             window.setTimeout(function()      
  50.             {      
  51.                 if(parseInt($image.attr('width')) < 20)      
  52.                 {         
  53.                     jQuery($select).html('');      
  54.                 }      
  55.             },500);      
  56.         });      
  57.     });      
  58.        
  59. });  
  60. wp_enqueue_script('kriesi_custom_fields_js', get_template_directory_uri(). '/js/metaup.js');  
  61. <?php   
  62. $new_meta_boxes =    
  63. array(   
  64.     "title" => array(   
  65.         "name" => "_meta_title",   
  66.         "std" => "",   
  67.         "title" => "标题",   
  68.         "type"=>"title"),      
  69.      
  70.     "keywords" => array(   
  71.         "name" => "_meta_keywords",   
  72.         "std" => "",      
  73.         "title" => "关键字",   
  74.         "type"=>"text"),   
  75.            
  76.     "description" => array(   
  77.         "name" => "_meta_description",   
  78.         "std" => "",      
  79.         "title" => "描述%

 第三步:加载js,要知道,如果仅仅加这个代码,虽然你能够调取到上传图片上传的对话框,但是还不能将图片url插入到文本域中,更不能实现实时预览。所以我们需要添加一个js文件,比如我沿用以前的,在twenty ten主题中新建一个js文件夹,在里面加你一个名为metaup.js的js文件,该js文件中的代码为:

  1. "uploads" => array(   
  2.         "name" => "_meta_uploader",   
  3.         "std" => '',      
  4.         "title" => "图片上传",   
  5.         "type"=>"uploader"),  
  6. case 'uploader':   
  7.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  8.                 //图片预览框   
  9.                 if($meta_box['std'] != ''){   
  10.                 echo '<span id="'.$meta_box['name'].'_value_img"><img src='.$meta_box['std'].' alt="" /></span>';}   
  11.                 echo '<input class="ashu_upload_input" type="text" size="40" value="'.$meta_box['std'].'" name="'.$meta_box['name'].'_value"/>';   
  12.                 echo '<input type="button" value="上传" class="ashu_bottom"/>';   
  13.                 echo '<br/>';   
  14.                 break;  
  15. jQuery(document).ready(function() {   
  16.     jQuery('input.ashu_bottom').click(function() {      
  17.         custom_editor = true;          
  18.          targetfield = jQuery(this).prev('input');      
  19.          tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');   
  20.          window.original_send_to_editor = window.send_to_editor;   
  21.          window.send_to_editor = function(html) {   
  22.             if (custom_editor) {       
  23.             imgurl = jQuery('img',html).attr('src');   
  24.             jQuery(targetfield).val(imgurl).focus();   
  25.             custom_editor = false;   
  26.             tb_remove();   
  27.             }else{   
  28.                 window.original_send_to_editor(html);   
  29.             }   
  30.         }           
  31.         return false;      
  32.     });      
  33.        
  34.        
  35.           
  36.     //图片实时预览ashu_upload_input为图片url文本框的class属性      
  37.     jQuery('input.ashu_upload_input').each(function()      
  38.     {      
  39.         jQuery(this).bind('change focus blur', function()      
  40.         {         
  41.             //获取改文本框的name属性后面      
  42.             $select = '#' + jQuery(this).attr('name') + '_img';      
  43.             $value = jQuery(this).val();      
  44.             $image = '<img src ="'+$value+'" />';      
  45.                               
  46.             var $image = jQuery($select).html('').append($image).find('img');      
  47.                   
  48.             //set timeout because of safari      
  49.             window.setTimeout(function()      
  50.             {      
  51.                 if(parseInt($image.attr('width')) < 20)      
  52.                 {         
  53.                     jQuery($select).html('');      
  54.                 }      
  55.             },500);      
  56.         });      
  57.     });      
  58.        
  59. });  
  60. wp_enqueue_script('kriesi_custom_fields_js', get_template_directory_uri(). '/js/metaup.js');  
  61. <?php   
  62. $new_meta_boxes =    
  63. array(   
  64.     "title" => array(   
  65.         "name" => "_meta_title",   
  66.         "std" => "",   
  67.         "title" => "标题",   
  68.         "type"=>"title"),      
  69.      
  70.     "keywords" => array(   
  71.         "name" => "_meta_keywords",   
  72.         "std" => "",      
  73.         "title" => "关键字",   
  74.         "type"=>"text"),   
  75.            
  76.     "description" => array(   
  77.         "name" => "_meta_description",   
  78.         "std" => "",      
  79.         "title" => "描述%

第四步:加载js文件,在metabox.php文件的函数中(请注意是在该函数中,不在switch语句的外面,或者你也可以直接放在这个文件中,不放在任何函数里面)添加以下代码:

  1. "uploads" => array(   
  2.         "name" => "_meta_uploader",   
  3.         "std" => '',      
  4.         "title" => "图片上传",   
  5.         "type"=>"uploader"),  
  6. case 'uploader':   
  7.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  8.                 //图片预览框   
  9.                 if($meta_box['std'] != ''){   
  10.                 echo '<span id="'.$meta_box['name'].'_value_img"><img src='.$meta_box['std'].' alt="" /></span>';}   
  11.                 echo '<input class="ashu_upload_input" type="text" size="40" value="'.$meta_box['std'].'" name="'.$meta_box['name'].'_value"/>';   
  12.                 echo '<input type="button" value="上传" class="ashu_bottom"/>';   
  13.                 echo '<br/>';   
  14.                 break;  
  15. jQuery(document).ready(function() {   
  16.     jQuery('input.ashu_bottom').click(function() {      
  17.         custom_editor = true;          
  18.          targetfield = jQuery(this).prev('input');      
  19.          tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');   
  20.          window.original_send_to_editor = window.send_to_editor;   
  21.          window.send_to_editor = function(html) {   
  22.             if (custom_editor) {       
  23.             imgurl = jQuery('img',html).attr('src');   
  24.             jQuery(targetfield).val(imgurl).focus();   
  25.             custom_editor = false;   
  26.             tb_remove();   
  27.             }else{   
  28.                 window.original_send_to_editor(html);   
  29.             }   
  30.         }           
  31.         return false;      
  32.     });      
  33.        
  34.        
  35.           
  36.     //图片实时预览ashu_upload_input为图片url文本框的class属性      
  37.     jQuery('input.ashu_upload_input').each(function()      
  38.     {      
  39.         jQuery(this).bind('change focus blur', function()      
  40.         {         
  41.             //获取改文本框的name属性后面      
  42.             $select = '#' + jQuery(this).attr('name') + '_img';      
  43.             $value = jQuery(this).val();      
  44.             $image = '<img src ="'+$value+'" />';      
  45.                               
  46.             var $image = jQuery($select).html('').append($image).find('img');      
  47.                   
  48.             //set timeout because of safari      
  49.             window.setTimeout(function()      
  50.             {      
  51.                 if(parseInt($image.attr('width')) < 20)      
  52.                 {         
  53.                     jQuery($select).html('');      
  54.                 }      
  55.             },500);      
  56.         });      
  57.     });      
  58.        
  59. });  
  60. wp_enqueue_script('kriesi_custom_fields_js', get_template_directory_uri(). '/js/metaup.js');  
  61. <?php   
  62. $new_meta_boxes =    
  63. array(   
  64.     "title" => array(   
  65.         "name" => "_meta_title",   
  66.         "std" => "",   
  67.         "title" => "标题",   
  68.         "type"=>"title"),      
  69.      
  70.     "keywords" => array(   
  71.         "name" => "_meta_keywords",   
  72.         "std" => "",      
  73.         "title" => "关键字",   
  74.         "type"=>"text"),   
  75.            
  76.     "description" => array(   
  77.         "name" => "_meta_description",   
  78.         "std" => "",      
  79.         "title" => "描述%

这样就完成了,效果图:

wordpress自定义面板中添加图片上传功能

调用方法请参考上一篇文章。

好了,为了方便懒人使用,将现在的metabox.php中的代码全部贴出,注意现在添加js文件才能使用哦。

  1. "uploads" => array(   
  2.         "name" => "_meta_uploader",   
  3.         "std" => '',      
  4.         "title" => "图片上传",   
  5.         "type"=>"uploader"),  
  6. case 'uploader':   
  7.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  8.                 //图片预览框   
  9.                 if($meta_box['std'] != ''){   
  10.                 echo '<span id="'.$meta_box['name'].'_value_img"><img src='.$meta_box['std'].' alt="" /></span>';}   
  11.                 echo '<input class="ashu_upload_input" type="text" size="40" value="'.$meta_box['std'].'" name="'.$meta_box['name'].'_value"/>';   
  12.                 echo '<input type="button" value="上传" class="ashu_bottom"/>';   
  13.                 echo '<br/>';   
  14.                 break;  
  15. jQuery(document).ready(function() {   
  16.     jQuery('input.ashu_bottom').click(function() {      
  17.         custom_editor = true;          
  18.          targetfield = jQuery(this).prev('input');      
  19.          tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');   
  20.          window.original_send_to_editor = window.send_to_editor;   
  21.          window.send_to_editor = function(html) {   
  22.             if (custom_editor) {       
  23.             imgurl = jQuery('img',html).attr('src');   
  24.             jQuery(targetfield).val(imgurl).focus();   
  25.             custom_editor = false;   
  26.             tb_remove();   
  27.             }else{   
  28.                 window.original_send_to_editor(html);   
  29.             }   
  30.         }           
  31.         return false;      
  32.     });      
  33.        
  34.        
  35.           
  36.     //图片实时预览ashu_upload_input为图片url文本框的class属性      
  37.     jQuery('input.ashu_upload_input').each(function()      
  38.     {      
  39.         jQuery(this).bind('change focus blur', function()      
  40.         {         
  41.             //获取改文本框的name属性后面      
  42.             $select = '#' + jQuery(this).attr('name') + '_img';      
  43.             $value = jQuery(this).val();      
  44.             $image = '<img src ="'+$value+'" />';      
  45.                               
  46.             var $image = jQuery($select).html('').append($image).find('img');      
  47.                   
  48.             //set timeout because of safari      
  49.             window.setTimeout(function()      
  50.             {      
  51.                 if(parseInt($image.attr('width')) < 20)      
  52.                 {         
  53.                     jQuery($select).html('');      
  54.                 }      
  55.             },500);      
  56.         });      
  57.     });      
  58.        
  59. });  
  60. wp_enqueue_script('kriesi_custom_fields_js', get_template_directory_uri(). '/js/metaup.js');  
  61. <?php   
  62. $new_meta_boxes =    
  63. array(   
  64.     "title" => array(   
  65.         "name" => "_meta_title",   
  66.         "std" => "",   
  67.         "title" => "标题",   
  68.         "type"=>"title"),      
  69.      
  70.     "keywords" => array(   
  71.         "name" => "_meta_keywords",   
  72.         "std" => "",      
  73.         "title" => "关键字",   
  74.         "type"=>"text"),   
  75.            
  76.     "description" => array(   
  77.         "name" => "_meta_description",   
  78.         "std" => "",      
  79.         "title" => "描述%