wordpress进阶教程(七):自定义面板添加下拉框、选择框等表单项


上一篇文章,我们在wordpress后台文章编辑页面添加了自定义面板,并且在自定义面板中可以输入关键词和描述信息,但是我们仅仅使用了一个textarea文本域表单,这在实际应用中是远远不够的,实际应用我们可能需要文本框、文本域、单选框、复选框、下拉选择框、图片上传、甚至是wordpress自带的文本编辑器或者自己配置的文本编辑器以及各种带js特效的应用。

今天我们给自定义面板添加简单的表单:文本框、文本域、单选框、复选框、下拉选择框。

我们继续使用昨天的文件,改进昨天的代码。

首先准备工作:

先将昨天新建的metabox.php文件中的代码清楚,准备字段数组:我们可以参考前面后台教程中配置选项数组的方法来配置数据项:设置选项类文件使用方法:

  1. $new_meta_boxes =    
  2. array(   
  3.     "title" => array(   
  4.         "name" => "_meta_title",   
  5.         "std" => "",   
  6.         "title" => "标题",   
  7.         "type"=>"title"),      
  8.      
  9.     "keywords" => array(   
  10.         "name" => "_meta_keywords",   
  11.         "std" => "",      
  12.         "title" => "关键字",   
  13.         "type"=>"text"),   
  14.            
  15.     "description" => array(   
  16.         "name" => "_meta_description",   
  17.         "std" => "",      
  18.         "title" => "描述",   
  19.         "type"=>"textarea"),   
  20.            
  21.     "category" => array(   
  22.         "name" => "_meta_cate",   
  23.         "std" => "",      
  24.         "title" => "选择分类",   
  25.         "subtype"=> "cat",   
  26.         "type"=>"dropdown"),   
  27.            
  28.     "radio" => array(   
  29.         "name" => "_meta_radio",   
  30.         "std" => 1,      
  31.         "title" => "单选框",   
  32.         "buttons" => array('Yes','No'),   
  33.         "type"=>"radio"),   
  34.            
  35.     "checkbox" => array(   
  36.         "name" => "_meta_checkbox",   
  37.         "std" => 1,      
  38.         "title" => "复选框",   
  39.         "type"=>"checkbox"),   
  40.            
  41. );  
  42. function new_meta_boxes() {   
  43.     global $post, $new_meta_boxes;   
  44.     foreach($new_meta_boxes as $meta_box) {   
  45.         //获取保存的是   
  46.         $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);   
  47.         if($meta_box_value != "")      
  48.             $meta_box['std'] = $meta_box_value;//将默认值替换为以保存的值   
  49.            
  50.         echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';   
  51.         //通过选择类型输出不同的html代码   
  52.         switch ( $meta_box['type'] ){   
  53.             case 'title':   
  54.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  55.                 break;   
  56.             case 'text':   
  57.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  58.                 echo '<input type="text" size="40" name="'.$meta_box['name'].'_value" value="'.$meta_box['std'].'" /><br />';   
  59.                 break;   
  60.             case 'textarea':   
  61.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  62.                 echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box['std'].'</textarea><br />';   
  63.                 break;   
  64.             case 'dropdown':   
  65.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  66.                 if($meta_box['subtype'] == 'cat'){   
  67.                     $select = 'Select category';   
  68.                     $entries = get_categories('title_li=&orderby=name&hide_empty=0');//获取分类   
  69.                 }   
  70.                 echo '<p><select name="'.$meta_box['name'].'_value"> ';   
  71.                 echo '<option value="">'.$select .'</option>  ';   
  72.                 foreach ($entries as $key => $entry){   
  73.                     $id = $entry->term_id;   
  74.                     $title = $entry->name;   
  75.                     if ( $meta_box['std'] == $id ){   
  76.                         $selected = "selected='selected'";   
  77.                     }else{   
  78.                         $selected = "";   
  79.                     }   
  80.                     echo "<option $selected value='". $id."'>". $title."</option>";   
  81.                 }   
  82.                 echo '</select><br />';   
  83.                 break;   
  84.             case 'radio':   
  85.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  86.                 $counter = 1;   
  87.                 foreach( $meta_box['buttons'] as $radiobutton ) {   
  88.                     $checked ="";   
  89.                     if(isset($meta_box['std']) && $meta_box['std'] == $counter) {   
  90.                         $checked = 'checked = "checked"';   
  91.                     }   
  92.                     echo '<input '.$checked.' type="radio" class="kcheck" value="'.$counter.'" name="'.$meta_box['name'].'_value"/>'.$radiobutton;   
  93.                     $counter++;   
  94.                 }   
  95.                 break;   
  96.             case 'checkbox':   
  97.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  98.                 if( isset($meta_box['std']) && $meta_box['std'] == 'true' )   
  99.                     $checked = 'checked = "checked"';   
  100.                 else  
  101.                     $checked  = '';    
  102.                 echo '<input type="checkbox" name="'.$meta_box['name'].'_value" value="true"  '.$checked.' />';   
  103.             break;   
  104.                
  105.         }             
  106.     }      
  107. }  
  108. function create_meta_box() {      
  109.     global $theme_name;      
  110.      
  111.     if ( function_exists('add_meta_box') ) {      
  112.         add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' );      
  113.     }      
  114. }  
  115. function save_postdata( $post_id ) {      
  116.     global $post, $new_meta_boxes;      
  117.      
  118.     foreach($new_meta_boxes as $meta_box) {      
  119.         if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {      
  120.             return $post_id;      
  121.         }      
  122.      
  123.         if ( 'page' == $_POST['post_type'] ) {      
  124.             if ( !current_user_can( 'edit_page', $post_id ))      
  125.                 return $post_id;      
  126.         }       
  127.         else {      
  128.             if ( !current_user_can( 'edit_post', $post_id ))      
  129.                 return $post_id;      
  130.         }      
  131.      
  132.         $data = $_POST[$meta_box['name'].'_value'];      
  133.      
  134.         if(get_post_meta($post_id, $meta_box['name'].'_value') == "")      
  135.             add_post_meta($post_id, $meta_box['name'].'_value', $data, true);      
  136.         elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))      
  137.             update_post_meta($post_id, $meta_box['name'].'_value', $data);      
  138.         elseif($data == "")      
  139.             delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));      
  140.     }      
  141. }  
  142. add_action('admin_menu', 'create_meta_box');      
  143. add_action('save_post', 'save_postdata');   
  144. $ashu_eitor = get_post_meta($post->ID, "_meta_keywords_value", true);  

二、创建(显示)面板内容的函数

  1. $new_meta_boxes =    
  2. array(   
  3.     "title" => array(   
  4.         "name" => "_meta_title",   
  5.         "std" => "",   
  6.         "title" => "标题",   
  7.         "type"=>"title"),      
  8.      
  9.     "keywords" => array(   
  10.         "name" => "_meta_keywords",   
  11.         "std" => "",      
  12.         "title" => "关键字",   
  13.         "type"=>"text"),   
  14.            
  15.     "description" => array(   
  16.         "name" => "_meta_description",   
  17.         "std" => "",      
  18.         "title" => "描述",   
  19.         "type"=>"textarea"),   
  20.            
  21.     "category" => array(   
  22.         "name" => "_meta_cate",   
  23.         "std" => "",      
  24.         "title" => "选择分类",   
  25.         "subtype"=> "cat",   
  26.         "type"=>"dropdown"),   
  27.            
  28.     "radio" => array(   
  29.         "name" => "_meta_radio",   
  30.         "std" => 1,      
  31.         "title" => "单选框",   
  32.         "buttons" => array('Yes','No'),   
  33.         "type"=>"radio"),   
  34.            
  35.     "checkbox" => array(   
  36.         "name" => "_meta_checkbox",   
  37.         "std" => 1,      
  38.         "title" => "复选框",   
  39.         "type"=>"checkbox"),   
  40.            
  41. );  
  42. function new_meta_boxes() {   
  43.     global $post, $new_meta_boxes;   
  44.     foreach($new_meta_boxes as $meta_box) {   
  45.         //获取保存的是   
  46.         $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);   
  47.         if($meta_box_value != "")      
  48.             $meta_box['std'] = $meta_box_value;//将默认值替换为以保存的值   
  49.            
  50.         echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';   
  51.         //通过选择类型输出不同的html代码   
  52.         switch ( $meta_box['type'] ){   
  53.             case 'title':   
  54.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  55.                 break;   
  56.             case 'text':   
  57.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  58.                 echo '<input type="text" size="40" name="'.$meta_box['name'].'_value" value="'.$meta_box['std'].'" /><br />';   
  59.                 break;   
  60.             case 'textarea':   
  61.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  62.                 echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box['std'].'</textarea><br />';   
  63.                 break;   
  64.             case 'dropdown':   
  65.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  66.                 if($meta_box['subtype'] == 'cat'){   
  67.                     $select = 'Select category';   
  68.                     $entries = get_categories('title_li=&orderby=name&hide_empty=0');//获取分类   
  69.                 }   
  70.                 echo '<p><select name="'.$meta_box['name'].'_value"> ';   
  71.                 echo '<option value="">'.$select .'</option>  ';   
  72.                 foreach ($entries as $key => $entry){   
  73.                     $id = $entry->term_id;   
  74.                     $title = $entry->name;   
  75.                     if ( $meta_box['std'] == $id ){   
  76.                         $selected = "selected='selected'";   
  77.                     }else{   
  78.                         $selected = "";   
  79.                     }   
  80.                     echo "<option $selected value='". $id."'>". $title."</option>";   
  81.                 }   
  82.                 echo '</select><br />';   
  83.                 break;   
  84.             case 'radio':   
  85.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  86.                 $counter = 1;   
  87.                 foreach( $meta_box['buttons'] as $radiobutton ) {   
  88.                     $checked ="";   
  89.                     if(isset($meta_box['std']) && $meta_box['std'] == $counter) {   
  90.                         $checked = 'checked = "checked"';   
  91.                     }   
  92.                     echo '<input '.$checked.' type="radio" class="kcheck" value="'.$counter.'" name="'.$meta_box['name'].'_value"/>'.$radiobutton;   
  93.                     $counter++;   
  94.                 }   
  95.                 break;   
  96.             case 'checkbox':   
  97.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  98.                 if( isset($meta_box['std']) && $meta_box['std'] == 'true' )   
  99.                     $checked = 'checked = "checked"';   
  100.                 else  
  101.                     $checked  = '';    
  102.                 echo '<input type="checkbox" name="'.$meta_box['name'].'_value" value="true"  '.$checked.' />';   
  103.             break;   
  104.                
  105.         }             
  106.     }      
  107. }  
  108. function create_meta_box() {      
  109.     global $theme_name;      
  110.      
  111.     if ( function_exists('add_meta_box') ) {      
  112.         add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' );      
  113.     }      
  114. }  
  115. function save_postdata( $post_id ) {      
  116.     global $post, $new_meta_boxes;      
  117.      
  118.     foreach($new_meta_boxes as $meta_box) {      
  119.         if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {      
  120.             return $post_id;      
  121.         }      
  122.      
  123.         if ( 'page' == $_POST['post_type'] ) {      
  124.             if ( !current_user_can( 'edit_page', $post_id ))      
  125.                 return $post_id;      
  126.         }       
  127.         else {      
  128.             if ( !current_user_can( 'edit_post', $post_id ))      
  129.                 return $post_id;      
  130.         }      
  131.      
  132.         $data = $_POST[$meta_box['name'].'_value'];      
  133.      
  134.         if(get_post_meta($post_id, $meta_box['name'].'_value') == "")      
  135.             add_post_meta($post_id, $meta_box['name'].'_value', $data, true);      
  136.         elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))      
  137.             update_post_meta($post_id, $meta_box['name'].'_value', $data);      
  138.         elseif($data == "")      
  139.             delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));      
  140.     }      
  141. }  
  142. add_action('admin_menu', 'create_meta_box');      
  143. add_action('save_post', 'save_postdata');   
  144. $ashu_eitor = get_post_meta($post->ID, "_meta_keywords_value", true);  

三、创建面板

  1. $new_meta_boxes =    
  2. array(   
  3.     "title" => array(   
  4.         "name" => "_meta_title",   
  5.         "std" => "",   
  6.         "title" => "标题",   
  7.         "type"=>"title"),      
  8.      
  9.     "keywords" => array(   
  10.         "name" => "_meta_keywords",   
  11.         "std" => "",      
  12.         "title" => "关键字",   
  13.         "type"=>"text"),   
  14.            
  15.     "description" => array(   
  16.         "name" => "_meta_description",   
  17.         "std" => "",      
  18.         "title" => "描述",   
  19.         "type"=>"textarea"),   
  20.            
  21.     "category" => array(   
  22.         "name" => "_meta_cate",   
  23.         "std" => "",      
  24.         "title" => "选择分类",   
  25.         "subtype"=> "cat",   
  26.         "type"=>"dropdown"),   
  27.            
  28.     "radio" => array(   
  29.         "name" => "_meta_radio",   
  30.         "std" => 1,      
  31.         "title" => "单选框",   
  32.         "buttons" => array('Yes','No'),   
  33.         "type"=>"radio"),   
  34.            
  35.     "checkbox" => array(   
  36.         "name" => "_meta_checkbox",   
  37.         "std" => 1,      
  38.         "title" => "复选框",   
  39.         "type"=>"checkbox"),   
  40.            
  41. );  
  42. function new_meta_boxes() {   
  43.     global $post, $new_meta_boxes;   
  44.     foreach($new_meta_boxes as $meta_box) {   
  45.         //获取保存的是   
  46.         $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);   
  47.         if($meta_box_value != "")      
  48.             $meta_box['std'] = $meta_box_value;//将默认值替换为以保存的值   
  49.            
  50.         echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';   
  51.         //通过选择类型输出不同的html代码   
  52.         switch ( $meta_box['type'] ){   
  53.             case 'title':   
  54.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  55.                 break;   
  56.             case 'text':   
  57.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  58.                 echo '<input type="text" size="40" name="'.$meta_box['name'].'_value" value="'.$meta_box['std'].'" /><br />';   
  59.                 break;   
  60.             case 'textarea':   
  61.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  62.                 echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box['std'].'</textarea><br />';   
  63.                 break;   
  64.             case 'dropdown':   
  65.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  66.                 if($meta_box['subtype'] == 'cat'){   
  67.                     $select = 'Select category';   
  68.                     $entries = get_categories('title_li=&orderby=name&hide_empty=0');//获取分类   
  69.                 }   
  70.                 echo '<p><select name="'.$meta_box['name'].'_value"> ';   
  71.                 echo '<option value="">'.$select .'</option>  ';   
  72.                 foreach ($entries as $key => $entry){   
  73.                     $id = $entry->term_id;   
  74.                     $title = $entry->name;   
  75.                     if ( $meta_box['std'] == $id ){   
  76.                         $selected = "selected='selected'";   
  77.                     }else{   
  78.                         $selected = "";   
  79.                     }   
  80.                     echo "<option $selected value='". $id."'>". $title."</option>";   
  81.                 }   
  82.                 echo '</select><br />';   
  83.                 break;   
  84.             case 'radio':   
  85.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  86.                 $counter = 1;   
  87.                 foreach( $meta_box['buttons'] as $radiobutton ) {   
  88.                     $checked ="";   
  89.                     if(isset($meta_box['std']) && $meta_box['std'] == $counter) {   
  90.                         $checked = 'checked = "checked"';   
  91.                     }   
  92.                     echo '<input '.$checked.' type="radio" class="kcheck" value="'.$counter.'" name="'.$meta_box['name'].'_value"/>'.$radiobutton;   
  93.                     $counter++;   
  94.                 }   
  95.                 break;   
  96.             case 'checkbox':   
  97.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  98.                 if( isset($meta_box['std']) && $meta_box['std'] == 'true' )   
  99.                     $checked = 'checked = "checked"';   
  100.                 else  
  101.                     $checked  = '';    
  102.                 echo '<input type="checkbox" name="'.$meta_box['name'].'_value" value="true"  '.$checked.' />';   
  103.             break;   
  104.                
  105.         }             
  106.     }      
  107. }  
  108. function create_meta_box() {      
  109.     global $theme_name;      
  110.      
  111.     if ( function_exists('add_meta_box') ) {      
  112.         add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' );      
  113.     }      
  114. }  
  115. function save_postdata( $post_id ) {      
  116.     global $post, $new_meta_boxes;      
  117.      
  118.     foreach($new_meta_boxes as $meta_box) {      
  119.         if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {      
  120.             return $post_id;      
  121.         }      
  122.      
  123.         if ( 'page' == $_POST['post_type'] ) {      
  124.             if ( !current_user_can( 'edit_page', $post_id ))      
  125.                 return $post_id;      
  126.         }       
  127.         else {      
  128.             if ( !current_user_can( 'edit_post', $post_id ))      
  129.                 return $post_id;      
  130.         }      
  131.      
  132.         $data = $_POST[$meta_box['name'].'_value'];      
  133.      
  134.         if(get_post_meta($post_id, $meta_box['name'].'_value') == "")      
  135.             add_post_meta($post_id, $meta_box['name'].'_value', $data, true);      
  136.         elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))      
  137.             update_post_meta($post_id, $meta_box['name'].'_value', $data);      
  138.         elseif($data == "")      
  139.             delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));      
  140.     }      
  141. }  
  142. add_action('admin_menu', 'create_meta_box');      
  143. add_action('save_post', 'save_postdata');   
  144. $ashu_eitor = get_post_meta($post->ID, "_meta_keywords_value", true);  

四、保存更新数据

  1. $new_meta_boxes =    
  2. array(   
  3.     "title" => array(   
  4.         "name" => "_meta_title",   
  5.         "std" => "",   
  6.         "title" => "标题",   
  7.         "type"=>"title"),      
  8.      
  9.     "keywords" => array(   
  10.         "name" => "_meta_keywords",   
  11.         "std" => "",      
  12.         "title" => "关键字",   
  13.         "type"=>"text"),   
  14.            
  15.     "description" => array(   
  16.         "name" => "_meta_description",   
  17.         "std" => "",      
  18.         "title" => "描述",   
  19.         "type"=>"textarea"),   
  20.            
  21.     "category" => array(   
  22.         "name" => "_meta_cate",   
  23.         "std" => "",      
  24.         "title" => "选择分类",   
  25.         "subtype"=> "cat",   
  26.         "type"=>"dropdown"),   
  27.            
  28.     "radio" => array(   
  29.         "name" => "_meta_radio",   
  30.         "std" => 1,      
  31.         "title" => "单选框",   
  32.         "buttons" => array('Yes','No'),   
  33.         "type"=>"radio"),   
  34.            
  35.     "checkbox" => array(   
  36.         "name" => "_meta_checkbox",   
  37.         "std" => 1,      
  38.         "title" => "复选框",   
  39.         "type"=>"checkbox"),   
  40.            
  41. );  
  42. function new_meta_boxes() {   
  43.     global $post, $new_meta_boxes;   
  44.     foreach($new_meta_boxes as $meta_box) {   
  45.         //获取保存的是   
  46.         $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);   
  47.         if($meta_box_value != "")      
  48.             $meta_box['std'] = $meta_box_value;//将默认值替换为以保存的值   
  49.            
  50.         echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';   
  51.         //通过选择类型输出不同的html代码   
  52.         switch ( $meta_box['type'] ){   
  53.             case 'title':   
  54.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  55.                 break;   
  56.             case 'text':   
  57.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  58.                 echo '<input type="text" size="40" name="'.$meta_box['name'].'_value" value="'.$meta_box['std'].'" /><br />';   
  59.                 break;   
  60.             case 'textarea':   
  61.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  62.                 echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box['std'].'</textarea><br />';   
  63.                 break;   
  64.             case 'dropdown':   
  65.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  66.                 if($meta_box['subtype'] == 'cat'){   
  67.                     $select = 'Select category';   
  68.                     $entries = get_categories('title_li=&orderby=name&hide_empty=0');//获取分类   
  69.                 }   
  70.                 echo '<p><select name="'.$meta_box['name'].'_value"> ';   
  71.                 echo '<option value="">'.$select .'</option>  ';   
  72.                 foreach ($entries as $key => $entry){   
  73.                     $id = $entry->term_id;   
  74.                     $title = $entry->name;   
  75.                     if ( $meta_box['std'] == $id ){   
  76.                         $selected = "selected='selected'";   
  77.                     }else{   
  78.                         $selected = "";   
  79.                     }   
  80.                     echo "<option $selected value='". $id."'>". $title."</option>";   
  81.                 }   
  82.                 echo '</select><br />';   
  83.                 break;   
  84.             case 'radio':   
  85.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  86.                 $counter = 1;   
  87.                 foreach( $meta_box['buttons'] as $radiobutton ) {   
  88.                     $checked ="";   
  89.                     if(isset($meta_box['std']) && $meta_box['std'] == $counter) {   
  90.                         $checked = 'checked = "checked"';   
  91.                     }   
  92.                     echo '<input '.$checked.' type="radio" class="kcheck" value="'.$counter.'" name="'.$meta_box['name'].'_value"/>'.$radiobutton;   
  93.                     $counter++;   
  94.                 }   
  95.                 break;   
  96.             case 'checkbox':   
  97.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  98.                 if( isset($meta_box['std']) && $meta_box['std'] == 'true' )   
  99.                     $checked = 'checked = "checked"';   
  100.                 else  
  101.                     $checked  = '';    
  102.                 echo '<input type="checkbox" name="'.$meta_box['name'].'_value" value="true"  '.$checked.' />';   
  103.             break;   
  104.                
  105.         }             
  106.     }      
  107. }  
  108. function create_meta_box() {      
  109.     global $theme_name;      
  110.      
  111.     if ( function_exists('add_meta_box') ) {      
  112.         add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' );      
  113.     }      
  114. }  
  115. function save_postdata( $post_id ) {      
  116.     global $post, $new_meta_boxes;      
  117.      
  118.     foreach($new_meta_boxes as $meta_box) {      
  119.         if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {      
  120.             return $post_id;      
  121.         }      
  122.      
  123.         if ( 'page' == $_POST['post_type'] ) {      
  124.             if ( !current_user_can( 'edit_page', $post_id ))      
  125.                 return $post_id;      
  126.         }       
  127.         else {      
  128.             if ( !current_user_can( 'edit_post', $post_id ))      
  129.                 return $post_id;      
  130.         }      
  131.      
  132.         $data = $_POST[$meta_box['name'].'_value'];      
  133.      
  134.         if(get_post_meta($post_id, $meta_box['name'].'_value') == "")      
  135.             add_post_meta($post_id, $meta_box['name'].'_value', $data, true);      
  136.         elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))      
  137.             update_post_meta($post_id, $meta_box['name'].'_value', $data);      
  138.         elseif($data == "")      
  139.             delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));      
  140.     }      
  141. }  
  142. add_action('admin_menu', 'create_meta_box');      
  143. add_action('save_post', 'save_postdata');   
  144. $ashu_eitor = get_post_meta($post->ID, "_meta_keywords_value", true);  

 五、触发函数

  1. $new_meta_boxes =    
  2. array(   
  3.     "title" => array(   
  4.         "name" => "_meta_title",   
  5.         "std" => "",   
  6.         "title" => "标题",   
  7.         "type"=>"title"),      
  8.      
  9.     "keywords" => array(   
  10.         "name" => "_meta_keywords",   
  11.         "std" => "",      
  12.         "title" => "关键字",   
  13.         "type"=>"text"),   
  14.            
  15.     "description" => array(   
  16.         "name" => "_meta_description",   
  17.         "std" => "",      
  18.         "title" => "描述",   
  19.         "type"=>"textarea"),   
  20.            
  21.     "category" => array(   
  22.         "name" => "_meta_cate",   
  23.         "std" => "",      
  24.         "title" => "选择分类",   
  25.         "subtype"=> "cat",   
  26.         "type"=>"dropdown"),   
  27.            
  28.     "radio" => array(   
  29.         "name" => "_meta_radio",   
  30.         "std" => 1,      
  31.         "title" => "单选框",   
  32.         "buttons" => array('Yes','No'),   
  33.         "type"=>"radio"),   
  34.            
  35.     "checkbox" => array(   
  36.         "name" => "_meta_checkbox",   
  37.         "std" => 1,      
  38.         "title" => "复选框",   
  39.         "type"=>"checkbox"),   
  40.            
  41. );  
  42. function new_meta_boxes() {   
  43.     global $post, $new_meta_boxes;   
  44.     foreach($new_meta_boxes as $meta_box) {   
  45.         //获取保存的是   
  46.         $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);   
  47.         if($meta_box_value != "")      
  48.             $meta_box['std'] = $meta_box_value;//将默认值替换为以保存的值   
  49.            
  50.         echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';   
  51.         //通过选择类型输出不同的html代码   
  52.         switch ( $meta_box['type'] ){   
  53.             case 'title':   
  54.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  55.                 break;   
  56.             case 'text':   
  57.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  58.                 echo '<input type="text" size="40" name="'.$meta_box['name'].'_value" value="'.$meta_box['std'].'" /><br />';   
  59.                 break;   
  60.             case 'textarea':   
  61.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  62.                 echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box['std'].'</textarea><br />';   
  63.                 break;   
  64.             case 'dropdown':   
  65.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  66.                 if($meta_box['subtype'] == 'cat'){   
  67.                     $select = 'Select category';   
  68.                     $entries = get_categories('title_li=&orderby=name&hide_empty=0');//获取分类   
  69.                 }   
  70.                 echo '<p><select name="'.$meta_box['name'].'_value"> ';   
  71.                 echo '<option value="">'.$select .'</option>  ';   
  72.                 foreach ($entries as $key => $entry){   
  73.                     $id = $entry->term_id;   
  74.                     $title = $entry->name;   
  75.                     if ( $meta_box['std'] == $id ){   
  76.                         $selected = "selected='selected'";   
  77.                     }else{   
  78.                         $selected = "";   
  79.                     }   
  80.                     echo "<option $selected value='". $id."'>". $title."</option>";   
  81.                 }   
  82.                 echo '</select><br />';   
  83.                 break;   
  84.             case 'radio':   
  85.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  86.                 $counter = 1;   
  87.                 foreach( $meta_box['buttons'] as $radiobutton ) {   
  88.                     $checked ="";   
  89.                     if(isset($meta_box['std']) && $meta_box['std'] == $counter) {   
  90.                         $checked = 'checked = "checked"';   
  91.                     }   
  92.                     echo '<input '.$checked.' type="radio" class="kcheck" value="'.$counter.'" name="'.$meta_box['name'].'_value"/>'.$radiobutton;   
  93.                     $counter++;   
  94.                 }   
  95.                 break;   
  96.             case 'checkbox':   
  97.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  98.                 if( isset($meta_box['std']) && $meta_box['std'] == 'true' )   
  99.                     $checked = 'checked = "checked"';   
  100.                 else  
  101.                     $checked  = '';    
  102.                 echo '<input type="checkbox" name="'.$meta_box['name'].'_value" value="true"  '.$checked.' />';   
  103.             break;   
  104.                
  105.         }             
  106.     }      
  107. }  
  108. function create_meta_box() {      
  109.     global $theme_name;      
  110.      
  111.     if ( function_exists('add_meta_box') ) {      
  112.         add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' );      
  113.     }      
  114. }  
  115. function save_postdata( $post_id ) {      
  116.     global $post, $new_meta_boxes;      
  117.      
  118.     foreach($new_meta_boxes as $meta_box) {      
  119.         if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {      
  120.             return $post_id;      
  121.         }      
  122.      
  123.         if ( 'page' == $_POST['post_type'] ) {      
  124.             if ( !current_user_can( 'edit_page', $post_id ))      
  125.                 return $post_id;      
  126.         }       
  127.         else {      
  128.             if ( !current_user_can( 'edit_post', $post_id ))      
  129.                 return $post_id;      
  130.         }      
  131.      
  132.         $data = $_POST[$meta_box['name'].'_value'];      
  133.      
  134.         if(get_post_meta($post_id, $meta_box['name'].'_value') == "")      
  135.             add_post_meta($post_id, $meta_box['name'].'_value', $data, true);      
  136.         elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))      
  137.             update_post_meta($post_id, $meta_box['name'].'_value', $data);      
  138.         elseif($data == "")      
  139.             delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));      
  140.     }      
  141. }  
  142. add_action('admin_menu', 'create_meta_box');      
  143. add_action('save_post', 'save_postdata');   
  144. $ashu_eitor = get_post_meta($post->ID, "_meta_keywords_value", true);  

效果:

wordpress自定义面板

调用的时候使用get_post_meta函数获取即可,不过主要我们保存的字段名称,虽然我们的配置数组中配置了name,但是保存自定义字段的时候我们在字段名称后面加了_value,以我们的关键字_meta_keywords为例,获取应该是:

  1. $new_meta_boxes =    
  2. array(   
  3.     "title" => array(   
  4.         "name" => "_meta_title",   
  5.         "std" => "",   
  6.         "title" => "标题",   
  7.         "type"=>"title"),      
  8.      
  9.     "keywords" => array(   
  10.         "name" => "_meta_keywords",   
  11.         "std" => "",      
  12.         "title" => "关键字",   
  13.         "type"=>"text"),   
  14.            
  15.     "description" => array(   
  16.         "name" => "_meta_description",   
  17.         "std" => "",      
  18.         "title" => "描述",   
  19.         "type"=>"textarea"),   
  20.            
  21.     "category" => array(   
  22.         "name" => "_meta_cate",   
  23.         "std" => "",      
  24.         "title" => "选择分类",   
  25.         "subtype"=> "cat",   
  26.         "type"=>"dropdown"),   
  27.            
  28.     "radio" => array(   
  29.         "name" => "_meta_radio",   
  30.         "std" => 1,      
  31.         "title" => "单选框",   
  32.         "buttons" => array('Yes','No'),   
  33.         "type"=>"radio"),   
  34.            
  35.     "checkbox" => array(   
  36.         "name" => "_meta_checkbox",   
  37.         "std" => 1,      
  38.         "title" => "复选框",   
  39.         "type"=>"checkbox"),   
  40.            
  41. );  
  42. function new_meta_boxes() {   
  43.     global $post, $new_meta_boxes;   
  44.     foreach($new_meta_boxes as $meta_box) {   
  45.         //获取保存的是   
  46.         $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);   
  47.         if($meta_box_value != "")      
  48.             $meta_box['std'] = $meta_box_value;//将默认值替换为以保存的值   
  49.            
  50.         echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';   
  51.         //通过选择类型输出不同的html代码   
  52.         switch ( $meta_box['type'] ){   
  53.             case 'title':   
  54.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  55.                 break;   
  56.             case 'text':   
  57.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  58.                 echo '<input type="text" size="40" name="'.$meta_box['name'].'_value" value="'.$meta_box['std'].'" /><br />';   
  59.                 break;   
  60.             case 'textarea':   
  61.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  62.                 echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box['std'].'</textarea><br />';   
  63.                 break;   
  64.             case 'dropdown':   
  65.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  66.                 if($meta_box['subtype'] == 'cat'){   
  67.                     $select = 'Select category';   
  68.                     $entries = get_categories('title_li=&orderby=name&hide_empty=0');//获取分类   
  69.                 }   
  70.                 echo '<p><select name="'.$meta_box['name'].'_value"> ';   
  71.                 echo '<option value="">'.$select .'</option>  ';   
  72.                 foreach ($entries as $key => $entry){   
  73.                     $id = $entry->term_id;   
  74.                     $title = $entry->name;   
  75.                     if ( $meta_box['std'] == $id ){   
  76.                         $selected = "selected='selected'";   
  77.                     }else{   
  78.                         $selected = "";   
  79.                     }   
  80.                     echo "<option $selected value='". $id."'>". $title."</option>";   
  81.                 }   
  82.                 echo '</select><br />';   
  83.                 break;   
  84.             case 'radio':   
  85.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  86.                 $counter = 1;   
  87.                 foreach( $meta_box['buttons'] as $radiobutton ) {   
  88.                     $checked ="";   
  89.                     if(isset($meta_box['std']) && $meta_box['std'] == $counter) {   
  90.                         $checked = 'checked = "checked"';   
  91.                     }   
  92.                     echo '<input '.$checked.' type="radio" class="kcheck" value="'.$counter.'" name="'.$meta_box['name'].'_value"/>'.$radiobutton;   
  93.                     $counter++;   
  94.                 }   
  95.                 break;   
  96.             case 'checkbox':   
  97.                 echo'<h4>'.$meta_box['title'].'</h4>';   
  98.                 if( isset($meta_box['std']) && $meta_box['std'] == 'true' )   
  99.                     $checked = 'checked = "checked"';   
  100.                 else  
  101.                     $checked  = '';    
  102.                 echo '<input type="checkbox" name="'.$meta_box['name'].'_value" value="true"  '.$checked.' />';   
  103.             break;   
  104.                
  105.         }             
  106.     }      
  107. }  
  108. function create_meta_box() {      
  109.     global $theme_name;      
  110.      
  111.     if ( function_exists('add_meta_box') ) {      
  112.         add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' );      
  113.     }      
  114. }  
  115. function save_postdata( $post_id ) {      
  116.     global $post, $new_meta_boxes;      
  117.      
  118.     foreach($new_meta_boxes as $meta_box) {      
  119.         if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {      
  120.             return $post_id;      
  121.         }      
  122.      
  123.         if ( 'page' == $_POST['post_type'] ) {      
  124.             if ( !current_user_can( 'edit_page', $post_id ))      
  125.                 return $post_id;      
  126.         }       
  127.         else {      
  128.             if ( !current_user_can( 'edit_post', $post_id ))      
  129.                 return $post_id;      
  130.         }      
  131.      
  132.         $data = $_POST[$meta_box['name'].'_value'];      
  133.      
  134.         if(get_post_meta($post_id, $meta_box['name'].'_value') == "")      
  135.             add_post_meta($post_id, $meta_box['name'].'_value', $data, true);      
  136.         elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))      
  137.             update_post_meta($post_id, $meta_box['name'].'_value', $data);      
  138.         elseif($data == "")      
  139.             delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));      
  140.     }      
  141. }  
  142. add_action('admin_menu', 'create_meta_box');      
  143. add_action('save_post', 'save_postdata');   
  144. $ashu_eitor = get_post_meta($post->ID, "_meta_keywords_value", true);  

 

其实本教程的代码很多和前面的相同,只不过给准备的数据加了个类型判断,通过判断类型来输出不同的html代码。。