我從來沒見過在自定義面板新增編輯器的應用,直到昨天一個網友說有這個需求,但是我昨天嘗試了一下沒成功,原因是太晚了,腦袋不清醒,函式都沒寫完整。
好了廢話不多說,怎樣在 WordPress 的自定義面板中新增一個視覺化的編輯器呢?我們還是沿用上一篇教程中的測試檔案,直接在該檔案中新增一項。
首先開啟我們的 metabox.php 檔案,在配置陣列中新增一項:
- "checkbox" => array(
- "name" => "_meta_eidtor",
- "std" => '',
- "title" => "編輯器",
- "type"=>"editor"),
接下來我們只需要改動顯示的函式 new_meta_boxes,在它的 switch 語句中新增一項即可:
- case 'editor':
- echo'<h4>'.$meta_box['title'].'</h4>';
- wp_editor( $meta_box['std'], $meta_box['name'].'_value' );
- //帶配置引數
- /*wp_editor($meta_box['std'],$meta_box['name'].'_value', $settings = array(quicktags=>0,//取消 html 模式
- tinymce=>1,//視覺化模式
- media_buttons=>0,//取消媒體上傳
- textarea_rows=>5,//行數設為 5
- editor_class=>"textareastyle") ); */
- break;
其實呼叫編輯器很簡單,只需要使用 wp_editor 函式即可。
效果圖:

至此,為了方便很多不願意折騰的訪客,我們直接貼出到現在為止的整個 metabox.php 檔案的程式碼,你只需要在主題中載入這個檔案即可,至於怎麼配置,我想無需多說。
- <?php
- $new_meta_boxes =
- array(
- "title" => array(
- "name" => "_meta_title",
- "std" => "",
- "title" => "標題",
- "type"=>"title"),
- "keywords" => array(
- "name" => "_meta_keywords",
- "std" => "",
- "title" => "關鍵字",
- "type"=>"text"),
- "description" => array(
- "name" => "_meta_description",
- "std" => "",
- "title" => "描述",
- "type"=>"textarea"),
- "category" => array(
- "name" => "_meta_cate",
- "std" => "",
- "title" => "選擇分類",
- "subtype"=> "cat",
- "type"=>"dropdown"),
- "radio" => array(
- "name" => "_meta_radio",
- "std" => 1,
- "title" => "單選框",
- "buttons" => array('Yes','No'),
- "type"=>"radio"),
- "checkbox" => array(
- "name" => "_meta_checkbox",
- "std" => 1,
- "title" => "核取方塊",
- "type"=>"checkbox"),
- "checkbox" => array(
- "name" => "_meta_eidtor",
- "std" => '',
- "title" => "編輯器",
- "type"=>"editor"),
- );
- function new_meta_boxes() {
- global $post, $new_meta_boxes;
- foreach($new_meta_boxes as $meta_box) {
- //獲取儲存的是
- $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
- if($meta_box_value != "")
- $meta_box['std'] = $meta_box_value;//將預設值替換為以儲存的值
- echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
- //透過選擇型別輸出不同的 html 程式碼
- switch ( $meta_box['type'] ){
- case 'title':
- echo'<h4>'.$meta_box['title'].'</h4>';
- break;
- case 'text':
- echo'<h4>'.$meta_box['title'].'</h4>';
- echo '<input type="text" size="40" name="'.$meta_box['name'].'_value" value="'.$meta_box['std'].'" /><br />';
- break;
- case 'textarea':
- echo'<h4>'.$meta_box['title'].'</h4>';
- echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box['std'].'</textarea><br />';
- break;
- case 'dropdown':
- echo'<h4>'.$meta_box['title'].'</h4>';
- if($meta_box['subtype'] == 'cat'){
- $select = 'Select category';
- $entries = get_categories('title_li=&orderby=name&hide_empty=0');//獲取分類
- }
- echo '<p><select name="'.$meta_box['name'].'_value"> ';
- echo '<option value="">'.$select .'</option> ';
- foreach ($entries as $key => $entry){
- $id = $entry->term_id;
- $title = $entry->name;
- if ( $meta_box['std'] == $id ){
- $selected = "selected='selected'";
- }else{
- $selected = "";
- }
- echo "<option $selected value='". $id."'>". $title."</option>";
- }
- echo '</select><br />';
- break;
- case 'radio':
- echo'<h4>'.$meta_box['title'].'</h4>';
- $counter = 1;
- foreach( $meta_box['buttons'] as $radiobutton ) {
- $checked ="";
- if(isset($meta_box['std']) && $meta_box['std'] == $counter) {
- $checked = 'checked = "checked"';
- }
- echo '<input '.$checked.' type="radio" class="kcheck" value="'.$counter.'" name="'.$meta_box['name'].'_value"/>'.$radiobutton;
- $counter++;
- }
- break;
- case 'checkbox':
- echo'<h4>'.$meta_box['title'].'</h4>';
- if( isset($meta_box['std']) && $meta_box['std'] == 'true' )
- $checked = 'checked = "checked"';
- else
- $checked = '';
- echo '<input type="checkbox" name="'.$meta_box['name'].'_value" value="true" '.$checked.' />';
- break;
- //編輯器
- case 'editor':
- echo'<h4>'.$meta_box['title'].'</h4>';
- wp_editor( $meta_box['std'], $meta_box['name'].'_value' );
- //帶配置引數
- /*wp_editor($meta_box['std'],$meta_box['name'].'_value', $settings = array(quicktags=>0,//取消 html 模式
- tinymce=>1,//視覺化模式
- media_buttons=>0,//取消媒體上傳
- textarea_rows=>5,//行數設為 5
- editor_class=>"textareastyle") ); */
- break;
- }
- }
- }
- function create_meta_box() {
- global $theme_name;
- if ( function_exists('add_meta_box') ) {
- add_meta_box( 'new-meta-boxes', '自定義模組', 'new_meta_boxes', 'post', 'normal', 'high' );
- }
- }
- function save_postdata( $post_id ) {
- global $post, $new_meta_boxes;
- foreach($new_meta_boxes as $meta_box) {
- if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
- return $post_id;
- }
- if ( 'page' == $_POST['post_type'] ) {
- if ( !current_user_can( 'edit_page', $post_id ))
- return $post_id;
- }
- else {
- if ( !current_user_can( 'edit_post', $post_id ))
- return $post_id;
- }
- $data = $_POST[$meta_box['name'].'_value'];
- if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
- add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
- elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
- update_post_meta($post_id, $meta_box['name'].'_value', $data);
- elseif($data == "")
- delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
- }
- }
- add_action('admin_menu', 'create_meta_box');
- add_action('save_post', 'save_postdata');
- ?>
呼叫,使用 get_post_meta 函式獲取即可,不過主要我們儲存的欄位名稱,雖然我們的配置陣列中配置了 name,但是儲存自定義欄位的時候我們在欄位名稱後面加了_value,以我們今天新增的_meta_eidtor 為例,獲取應該是:
- $ashu_eidtor = get_post_meta($post->ID, "_meta_eidtor_value", true);
如何像普通文章一樣輸出?也就是自動加<p> 標籤和換行?如下:
- $ashu_editor = get_post_meta($post->ID, "_meta_eidtor_value", true);
- $ashu_editor = apply_filters('the_content', $ashu_editor);
- $ashu_editor = str_replace(']]>', ']]>', $ashu_editor);
- echo $ashu_editor;