WordPress客户自定义属性和自定义多个富文本工具

原创
小哥 3年前 (2022-11-02) 阅读数 92 #PHP
文章标签 PHP

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] : ;

?>

 true,) );?>

<?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();

参考:


        

输入分类封面图片URL

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] : ; */ ?>

输入分类封面图片URL

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

// $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 );
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门