wordpress进阶教程(十四):给分类添加字段类文件
敬告:本类文件发表于2013年,可能已经过时,请参考或直接使用Ashuwp_framework框架。
在这篇教程作者将要结束给wordpress分类添加字段的教程,教程最后当然是送出类文件,前面也放出过两个类文件:主题后台、文章自定义字段。
但是这个类文件不一样,该类文件为wp自学笔记原创,所以有不完美的地方,比如没有提供图片上传的,不过如果读者需要,参考一下前面主题后台和文章自定义字段的两个类文件,完全可以自己添加进来。
版本控制:
2013.07.08,版本 1.0
1、增加删除分类删除数据功能
2、增加和更新-编辑器、图片上传、复选框等功能
类文件代码-2013年7月8日更新:
- <?php
- /*
- wordpress分类法自定义字段类文件
- Version: 1.0
- Author: 树是我的朋友
- Author URI: http://www.ashuwp.com
- License: http://www.ashuwp.com/courses/highgrade/313.html
- */
- ashu_taxonomy_feild{
- $options;
- $taxonomyinfo;
- __construct($options,$taxonomyinfo){
- $this->options = $options;
- $this->taxonomyinfo = $taxonomyinfo;
- ($this->taxonomyinfo $taxonomy){
- add_action($taxonomy.’_add_form_fields’, (&$this, ‘taxonomy_fields_adds’), 10, 2);
- add_action($taxonomy.’_edit_form_fields’, (&$this, ‘taxonomy_metabox_edit’), 10, 2);
- add_action(‘created_’.$taxonomy, (&$this, ‘save_taxonomy_metadata’), 10, 1);
- add_action(‘edited_’.$taxonomy,(&$this, ‘save_taxonomy_metadata’), 10, 1);
- add_action(‘admin_menu’, (&$this, ‘init_taxonomy’));
- add_action(‘delete_’.$taxonomy, (&$this,’delete_taxonomy_metadata’),10,1);
- }
- }
- init_taxonomy(){
- (basename( $_SERVER[‘PHP_SELF’]) == “edit-tags.php”) {
- //注意加载的脚本的url
- wp_enqueue_style(‘cate_fields_css’, TEMJS_URI. ‘cate_fields.css’);
- wp_enqueue_script(‘cate_fields_js’,TEMJS_URI. ‘cate_fields.js’);
- wp_enqueue_script(‘thickbox’);
- wp_enqueue_style(‘thickbox’);
- }
- }
- /*********添加分类页面*******/
- taxonomy_fields_adds($tag){
- ($this->options $option){
- ( (!isset($option[“edit_only”]) || !$option[“edit_only”] ) ){
- (method_exists($this, $option[‘type’])){
- echo ‘<div =“form-field”>’;
- echo ‘<label =“‘.$option[‘id’].'” >’.$option[‘name’].'</label>’;
- $this->$option[‘type’]($option);
- echo ‘</div>’;
- }
- }
- }
- }
- /*************编辑分类页面*****************/
- taxonomy_metabox_edit($tag){
- ($this->options $option){
- (method_exists($this, $option[‘type’])){
- (get_term_meta($tag->term_id , $option[‘id’]) !== “”){
- $option[‘std’] = get_term_meta($tag->term_id,$option[‘id’], true);
- }
- echo ‘<tr =“form-field”>’;
- echo ‘<th scope=“row” valign=“top”>’;
- echo ‘<label =“‘.$option[‘id’].'” >’.$option[‘name’].'</label>’;
- echo ‘</th>’;
- echo ‘<td>’;
- $this->$option[‘type’]($option);
- echo ‘</td>’;
- echo ‘</tr>’;
- }
- }
- }
- /**********删除数据******/
- delete_taxonomy_metadata($term_id){
- ($this->options $option){
- delete_term_meta($term_id,$option[‘id’]);
- }
- }
- /**************保存数据*****************/
- save_taxonomy_metadata($term_id){
- ($this->options $option){
- (isset($_POST[$option[‘id’]])){
- (!current_user_can(‘manage_categories’)){
- $term_id ;
- }
- ( $option[‘type’] == ‘tinymce’ ){
- $data = stripslashes($_POST[$option[‘id’]]);
- }( $option[‘type’] == ‘checkbox’ ){
- $data = $_POST[$option[‘id’]];
- }{
- $data = htmlspecialchars($_POST[$option[‘id’]], ENT_QUOTES,“UTF-8”);
- }
- (get_term_meta($term_id , $option[‘id’]) == “”)
- add_term_meta($term_id , $option[‘id’], $data, true);
- ($data != get_term_meta($term_id , $option[‘id’], true))
- update_term_meta($term_id , $option[‘id’], $data);
- ($data == “”)
- delete_term_meta($term_id , $option[‘id’], get_term_meta($term_id , $option[‘id’], true));
- }
- }
- }
- /******提示信息******/
- info($option){
- echo ‘<p>’.$option[‘desc’].'</p>’;
- }
- /*******文本输入框text*******/
- text($option){
- echo ‘<input type=“text” size=“‘.$option[‘size’].'” value=“‘;
- echo $option[‘std’];
- echo ‘” id=“‘.$option[‘id’].'” name=“‘.$option[‘id’].'”/>’;
- echo ‘<p>’.$option[‘desc’].'</p>’;
- }
- /******文本域***********/
- textarea($option){
- echo ‘<textarea cols=“40” rows=“5” id=“‘.$option[‘id’].'” name=“‘.$option[‘id’].'”>’.$option[‘std’].'</textarea>’;
- echo ‘<p>’.$option[‘desc’].'</p>’;
- }
- /**********单选框************/
- radio( $option ){
- ( $option[‘buttons’] $key=>$value ) {
- $checked =“”;
- ( $option[‘std’] == $key) {
- $checked = ‘checked = “checked”‘;
- }
- echo ‘<input ‘.$checked.’ type=“radio” =“kcheck” value=“‘.$key.'” name=“‘.$option[‘id’].'”/>’.$value;
- }
- }
- /**************复选框*******************/
- checkbox($option) {
- ( $option[‘buttons’] $key=>$value ) {
- $checked =“”;
- ( is_array($option[‘std’]) && in_array($key,$option[‘std’])) {
- $checked = ‘checked = “checked”‘;
- }
- echo ‘<input ‘.$checked.’ type=“checkbox” =“kcheck” value=“‘.$key.'” name=“‘.$option[‘id’].'[]”/>’.$value;
- }
- }
- /***********下拉选择***********/
- dropdown($option){
- ($option[‘subtype’] == ‘page’) {
- $select = ‘Select page’;
- $entries = get_pages(‘title_li=&orderby=name’);
- } ($option[‘subtype’] == ‘sidebar’){
- $wp_registered_sidebars;
- $select = ‘Select a special sidebar’;
- $entries = $wp_registered_sidebars;
- } ($option[‘subtype’] == ‘cat’){
- $select = ‘Select category’;
- $entries = get_categories(‘title_li=&orderby=name&hide_empty=0’);
- }{
- $select = ‘Select…’;
- $entries = $option[‘subtype’];
- }
- echo ‘<select =“postform” id=“‘. $option[‘id’] .'” name=“‘. $option[‘id’] .'”> ‘;
- echo ‘<option value=“”>请选择…</option> ‘;
- ($entries $key => $entry){
- ($option[‘subtype’] == ‘page’){
- $id = $entry->ID;
- $title = $entry->post_title;
- } ($option[‘subtype’] == ‘cat’){
- $id = $entry->term_id;
- $title = $entry->name;
- } ($option[‘subtype’] == ‘sidebar’){
- $id = $entry[‘id’];
- $title = $entry[‘name’];
- }{
- $id = $key;
- $title = $entry;
- }
- $selected=”;
- ( $option[‘std’] == $id ){
- $selected = “selected=’selected'”;
- }
- echo“<option $selected value='”. $id .“‘>”. $title.“</option>”;
- }
- echo ‘</select>’;
- echo ‘<p>’.$option[‘desc’].'</p>’;
- }
- /***************图片上传**********************/
- upload($option){
- $prevImg = ”;
- ($option[‘std’] != ”){$prevImg = ‘<img src=’.$option[‘std’].’ alt=“” />’;}
- echo ‘<div =“preview_pic_optionspage” id=“‘.$option[‘id’].’_div”>’.$prevImg.'</div>’;
- echo $option[‘desc’].'<br/>’;
- echo ‘<input type=“text” size=“60” value=“‘.$option[‘std’].'” name=“‘.$option[‘id’].'” =“upload_pic_input” />’;
- echo ‘ <a onclick=“return false;” title=“” =“cat_up button thickbox” id=“‘.$option[‘id’].'” href=“media-upload.php?type=image&TB_iframe=true”>Insert Image</a>’;
- }
- /*******************编辑器****************************/
- tinymce($option){
- wp_editor( $option[‘std’], $option[‘id’],$settings=(‘tinymce’=>1,’media_buttons’=>0,) );
- }
- }
- ?>
下面提供类文件下载(共两个文件):wordpress给分类添加字段类文件其中一个类文件,一个simp-term-meta.php文件
配置示例:
注意:
- 其中配置数组中的edit_only参数如果设置为true,则该项将只会在分类的编辑页面出现,主要考虑是,在分类的添加页面,编辑器或者图片上传可能太大,所以一般将编辑器和图片上传设置为只在编辑页面出现。
- 复选框保存的数据为数组
- $options = ();
- $taxonomyinfo = (‘category’);
- $options[] = (
- “id” => “_notice”,
- “desc” => ‘提示信息内容’,
- “type” => “info”);
- $options[] = ( “name” => “分类关键词”,
- “desc” => “”,
- “id” => “_seo_keywords”,
- “std” => “”,
- “edit_only”=>false,
- “size” => 40,
- “type” => “text”);
- $options[] = ( “name” => “文本域测试”,
- “desc” => “描述信息”,
- “id” => “_textarea”,
- “std” => “”,
- “edit_only”=>false,
- “size” => 40,
- “type” => “textarea”);
- $options[] = ( “name” => “单选框测试”,
- “desc” => “请选择性别”,
- “id” => “_radio”,
- “std” => “sprother”,
- “edit_only”=>false,
- “buttons” => (
- ‘man’=>’男性’,
- ‘woman’=>’女性’,
- ‘sprother’=>’中性’
- ),
- “type” => “radio”);
- $options[] = ( “name” => “复选框–你喜欢哪几个”,
- “id” => “_checkbox”,
- “std” => “”,
- “buttons” => (
- ‘chun’=>’春哥’,
- ‘zeng’=>’曾哥’,
- ‘zhu’=>’著姐’,
- ‘feng’=>’玉凤’,
- ‘fu’=>’芙蓉’
- ),
- “edit_only”=>false,
- “type” => “checkbox”);
- $options[] = ( “name” => “下拉框框测试-页面”,
- “desc” => “描述信息”,
- “id” => “_drop_page”,
- “std” => “”,
- “edit_only”=>false,
- “subtype” => ‘page’,
- “type” => “dropdown”);
- $options[] = ( “name” => “下拉框框测试-分类”,
- “desc” => “描述信息”,
- “id” => “_drop_cat”,
- “std” => “”,
- “edit_only”=>false,
- “subtype” => ‘cat’,
- “type” => “dropdown”);
- $options[] = ( “name” => “下拉框框测试-侧边栏”,
- “desc” => “描述信息”,
- “id” => “_drop_sidebar”,
- “std” => “”,
- “edit_only”=>false,
- “subtype” => ‘sidebar’,
- “type” => “dropdown”);
- $options[] = ( “name” => “下拉框框测试-自定义”,
- “desc” => “描述信息”,
- “id” => “_drop_custom”,
- “std” => “”,
- “edit_only”=>false,
- “subtype” => (
- ‘apple’=>’苹果’,
- ‘banana’=>’香蕉’,
- ‘orange’=>’桔子’,
- ),
- “type” => “dropdown”);
- $options[] = (
- “name” => “上传图片测试”,
- “desc” => “”,
- “id” => “_upload”,
- “std” => “”,
- “edit_only”=>true,
- “type” => “upload”
- );
- $options[] = (
- “name” => “tinymce编辑器测试”,
- “desc” => “”,
- “id” => “_tinymce”,
- “std” => “”,
- “edit_only”=>true,
- “type” => “tinymce”
- );
- $new_taxonomy_feild = ashu_taxonomy_feild($options, $taxonomyinfo);
使用方法:
1、解压之后得到两个文件,在主题中包含这两个文件,分别是文件和simple-term-meta.php文件。
2、在你的主题中包含这两个文件这两个文件(如阿树将文件放在主题的include文件夹中,则使用如下代码):
- (get_template_directory() .’//-taxonomy-feild.php’);
- (get_template_directory() .’//simple-term-meta.php’);
2、类文件class-taxonomy-feild.php的后面带有类文件的实例化配置方法,按照需求更改即可。
3、获取数据,使用get_term_meta函数,跟get_post_meta函数类似。以上面示例中,分类关键词的获取为例:
- $_seo_keywords = get_term_meta( $cat->term_id, ‘_seo_keywords’,true );
FAQ:
1、能正确显示表单,但是无法保存数据?
本站提供的simple-term-meta.php文件在安装激活主题的时候,会在数据库中添加一个_termmeta表用来保存数据,如果无法保存数据,八成是未新建该数据表,请重新激活主题。
2、下载的压缩包是空的?
请换浏览器、换电脑。
3、如何使用该类文件给自定义分类法添加字段?
在配置代码中$taxonomyinfo数组,加入你自己的自定义分类法。