WordPress产品自定义字段和自定义多个富文本编辑器

74 篇文章 0 订阅

class Ludou_Tax_Image{
     
    function __construct(){
    } // __construct

    /**
     * 编辑分类页面添加自定义字段输入框
     *
     * @uses get_option()       从option表中获取option数据
     * @uses esc_url()          确保字符串是url
     */
    public function edit_tax_description_bottom_field( $term_id ){
        // 获取已保存的option
        $term_meta = get_option( "ludou_taxonomy_$term_id" );
        // option是一个二维数组
        $tax_description_bottom  = $term_meta['tax_description_bottom'] ? $term_meta['tax_description_bottom'] : '';
        
        
    ?>
              
        <tr class="form-field term-tax_description_bottom-wrap"> 
            <th scope="row"><label for="tax_description_bottom"><?php _e( 'Description bottom' ); ?></label></th>
            <td>
                <?php echo wp_editor($tax_description_bottom,  "tax_description_bottom", $settings = array('wpautop' =>  true,) );?>
            <p class="description"><?php _e( 'The description is not prominent by default; however, some themes may show it.' ); ?></p></td>
        </tr>  
        
    <?php
    } // edit_tax_image_field
 
    /**
     * 保存自定义字段的数据
     *
     * @uses get_option()      从option表中获取option数据
     * @uses update_option()   更新option数据,如果没有就新建option
     */
    public function save_tax_meta( $term_id ){
 
        if ( isset( $_POST['tax_description_bottom'] ) ) {
            
            // $term_id 是当前分类的id
            $t_id = $term_id;
            $term_meta = array();
            
            // 获取表单传过来的POST数据,POST数组一定要做过滤
            //$term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_url( $_POST['term_meta']['tax_image'] ) : '';
            $term_meta['tax_description_bottom'] = isset ( $_POST['tax_description_bottom'] ) ? $_POST['tax_description_bottom'] : '';

            /**
             *   TODO: 在这里追加获取其他自定义字段表单的值,如:
             *   $term_meta['tax_keywords'] = isset ( $_POST['term_meta']['tax_keywords'] ) ? $_POST['term_meta']['tax_keywords'] : '';
             */

            // 保存option数组
            update_option( "ludou_taxonomy_$t_id", $term_meta );
 
        } 
    } // save_tax_meta
 
} // Ludou_Tax_Image
 
$wptt_tax_image = new Ludou_Tax_Image();

参考:

<?php

class Ludou_Tax_Image{
 
    function __construct(){
        
        // 新建分类页面添加自定义字段输入框
        add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
        // 编辑分类页面添加自定义字段输入框
        add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );

        // 保存自定义字段数据
        add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
        add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );
 
 
    } // __construct
 
    /**
     * 新建分类页面添加自定义字段输入框
     */
    public function add_tax_image_field(){
    ?>
        <div class="form-field">
            <label for="term_meta[tax_image]">分类封面</label>
            <input type="text" name="term_meta[tax_image]" id="term_meta[tax_image]" value="" />
            <p class="description">输入分类封面图片URL</p>
        </div><!-- /.form-field -->
        
        <!-- TODO: 在这里追加其他自定义字段表单,如: -->
        
        <!--
        <div class="form-field">
            <label for="term_meta[tax_keywords]">分类关键字</label>
            <input type="text" name="term_meta[tax_keywords]" id="term_meta[tax_keywords]" value="" />
            <p class="description">输入分类关键字</p>
        </div>
        -->
    <?php
    } // add_tax_image_field
 
    /**
     * 编辑分类页面添加自定义字段输入框
     *
     * @uses get_option()       从option表中获取option数据
     * @uses esc_url()          确保字符串是url
     */
    public function edit_tax_image_field( $term ){
        
        // $term_id 是当前分类的id
        $term_id = $term->term_id;
        
        // 获取已保存的option
        $term_meta = get_option( "ludou_taxonomy_$term_id" );
        // option是一个二维数组
        $image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';
        
        /**
         *   TODO: 在这里追加获取其他自定义字段值,如:
         *   $keywords = $term_meta['tax_keywords'] ? $term_meta['tax_keywords'] : '';
         */
    ?>
        <tr class="form-field">
            <th scope="row">
                <label for="term_meta[tax_image]">分类封面</label>
                <td>
                    <input type="text" name="term_meta[tax_image]" id="term_meta[tax_image]" value="<?php echo esc_url( $image ); ?>" />
                    <p class="description">输入分类封面图片URL</p>
                </td>
            </th>
        </tr><!-- /.form-field -->
        
        <!-- TODO: 在这里追加其他自定义字段表单,如: -->
        
        <!--
        <tr class="form-field">
            <th scope="row">
                <label for="term_meta[tax_keywords]">分类关键字</label>
                <td>
                    <input type="text" name="term_meta[tax_keywords]" id="term_meta[tax_keywords]" value="<?php echo $keywords; ?>" />
                    <p class="description">输入分类关键字</p>
                </td>
            </th>
        </tr>
        -->
        
    <?php
    } // edit_tax_image_field
 
    /**
     * 保存自定义字段的数据
     *
     * @uses get_option()      从option表中获取option数据
     * @uses update_option()   更新option数据,如果没有就新建option
     */
    public function save_tax_meta( $term_id ){
 
        if ( isset( $_POST['term_meta'] ) ) {
            
            // $term_id 是当前分类的id
            $t_id = $term_id;
            $term_meta = array();
            
            // 获取表单传过来的POST数据,POST数组一定要做过滤
            $term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_url( $_POST['term_meta']['tax_image'] ) : '';

            /**
             *   TODO: 在这里追加获取其他自定义字段表单的值,如:
             *   $term_meta['tax_keywords'] = isset ( $_POST['term_meta']['tax_keywords'] ) ? $_POST['term_meta']['tax_keywords'] : '';
             */

            // 保存option数组
            update_option( "ludou_taxonomy_$t_id", $term_meta );
 
        } // if isset( $_POST['term_meta'] )
    } // save_tax_meta
 
} // Ludou_Tax_Image
 
$wptt_tax_image = new Ludou_Tax_Image();

如果需要在主题中调用分类自定义字段的值,可以使用以下代码:

// $term_id 是当前分类的id,自行想办法获取
$term_id = $term->term_id;
        
// 获取已保存的option
$term_meta = get_option( "ludou_taxonomy_$term_id" );

// 取值
$tax_image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';

全代码实现标签加字段

给标签添加自定义字段的原理是一样的,只需把上面第一部分代码中的action修改一下即可,将以上代码中的:

// 新建分类页面添加自定义字段输入框
add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
// 编辑分类页面添加自定义字段输入框
add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );

// 保存自定义字段数据
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );

改成:

// 其实就是把 category 改成 post_tag 即可
add_action( 'post_tag_add_form_fields', array( $this, 'add_tax_image_field' ) );
add_action( 'post_tag_edit_form_fields', array( $this, 'edit_tax_image_field' ) );

add_action( 'edited_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );

另外,也可以同时给分类目录和标签添加自定义字段:

// 分类
add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );


// 标签
add_action( 'post_tag_add_form_fields', array( $this, 'add_tax_image_field' ) );
add_action( 'post_tag_edit_form_fields', array( $this, 'edit_tax_image_field' ) );
add_action( 'edited_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅坞茶坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值