WordPress后臺開發(fā)過程中添加一個(gè)獨(dú)立的模塊,對添加文章相關(guān)的內(nèi)容會起到非常方便的作用,。
第一步,我們要添加代碼,,我們把代碼放在一個(gè)獨(dú)立的php文件中.
先新建一個(gè)php文件,,比如links_add_box.php ,,然后在function.php中添加代碼加載那個(gè)php文件:
include('admin/links_add_box.php');
文件名可隨意,注意路徑,。我這里兩個(gè)文件都在主題文件夾下。,。接下來的工作都是變價(jià)links_add_box.php文件,。。在links_add_box.php中只添加一對<?php ?>標(biāo)簽就行了,,后面所有代碼都<?php ?>之間。
第二步:首先添加下面兩行代碼,,兩個(gè)鉤子:
add_action('admin_menu', 'create_meta_box'); //在加載管理員界面界面的時(shí)候調(diào)用create_meta_box函數(shù) add_action('save_post', 'save_postdata'); //在保存文章的時(shí)候調(diào)用save_postdata函數(shù)
自定義模塊是在后臺,,所以在加載管理員界面的時(shí)候就調(diào)用函數(shù),我們添加這些信息主要是寫文章的時(shí)候,,所以保存文章的時(shí)候要調(diào)用函數(shù)保存文章數(shù)據(jù),。首先定義變量來存儲我們的信息:
$new_meta_box = array( "description" => array( "name"=>"description", "std"=>"默認(rèn)描述", "title"=>"描述:" ), "keywords" => array( "name"=>"keywords", "std"=>"默認(rèn)關(guān)鍵字", "title"=>"關(guān)鍵字:" ) );
接著添加create_meta_box函數(shù) :
function create_meta_box(){ //先判斷函數(shù)是否存在 if(function_exists('add_meta_box')){ //add_meta_box函數(shù)在文章編輯頁面內(nèi)添加版塊,具體用法放在文章最后 add_meta_box('new-meta-box','自定義模塊','new_meta_box','post','normal','high'); //此函數(shù)調(diào)用new_meta_box函數(shù) } }
再添加new_meta_box函數(shù) ,,這個(gè)函數(shù)就是定義模板樣式,,也就是輸出模板的html代碼:
function new_meta_box(){ global $post,$new_meta_box; foreach($new_meta_box as $meta_box){ $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true); if($meta_box_value == "") $meta_box_value = $meta_box['std']; echo '<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$mata_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'"/>'; echo '<h4>'.$meta_box['title'].'</h4>'; echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br/>'; } }
有了上面的函數(shù)你就可以在后臺看到自定義模板了。
下面就是我們開始添加的鉤子中的保存數(shù)據(jù)的鉤子,,也就是save_postdata函數(shù) :
function save_postdata($post_id){ global $post,$new_meta_box; foreach($new_meta_box 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)); } }
到了這里基本工作就完成了,,剩下的就是頁面調(diào)用了,更上一篇文章調(diào)用自定義字段一樣,。
但是注意這里我們在save_postdata函數(shù)中保存數(shù)據(jù)的函數(shù) add_post_meta()和更新數(shù)據(jù)的函數(shù) update_post_meta中,,字段名稱是$meta_box[‘name’]._value也就是分別是description_value 、keywords_value,,所以調(diào)用的時(shí)候:
$desc="description_value"; $key="keywords_value"; $description=get_post_meta($post->ID,$desc,true); $keywords=get_post_meta($post->ID,$key,true);
也可以在循環(huán)內(nèi)添加以下代碼
<?php echo get_post_meta($post->ID,"description_value",true);?>
即可調(diào)用成功,。
原文參考自:http://www.cnblogs.com/huangcong/archive/2012/07/16/2592894.html