WordPress产品自定义字段和自定义多个富文本编辑器 心已赠人 2021-09-08 17:04 608阅读 0赞 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMyNDIxNDg5_size_16_color_FFFFFF_t_70][] 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 ); [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMyNDIxNDg5_size_16_color_FFFFFF_t_70]: /images/20210908/dffa975c3e3e4499a815e899b8b07182.png
还没有评论,来说两句吧...