首先通过register_taxonomy函数创建自定义的分类。
function register_location_taxonomy() {
$args = array(
'labels' => array(
'name' => _x( 'Location', 'method' ),
'singular_name' => _x( 'Location', 'method' ),
'add_new_item' => __( 'Add New Location', 'method' ),
),
'public' => true,
);
register_taxonomy( 'location', 'post', $args );
}
然后添加到init的钩子上。add_action(‘init’,’register_location_taxonomy’); 完整可自定义的代码在这:https://github.com/asianwayne/custom_taxonomy/blob/main/custom_taxonomy.php
这样会在post那里添加location的一个分类。
taxonomy和category的理解:如果category里面有个分类,car. category是taxonomy, car是category.
添加分类字段的流程和方式跟添加post meta差不多。不过分类字段的话,有两个屏幕,一个是添加分类时候的屏幕,一个是编辑字段时候的屏幕。
添加分类时候屏幕的钩子是{taxonomy}_add_form_fields, {taxonomy}意思是指要添加钩子的位置,比方说category的话,就是category_add_foemd_fields, 比方说这里是自定义的taxonomy location的话,就是location_add_form_fields,.
创建自定义函数add_location_metadata(),并添加到钩子 location_add_form_fields 上。
add_action('location_add_form_fields','add_location_metadata');
在自定义函数 add_location_metadata ()里面,添加要输入的input框。
首先要创建nonce, 来实现字段保存时候的验证,从而来保证安全。然后再添加Input框,input的name值就是我们需要保存的字段。
function add_location_metadata() {
wp_nonce_field(basename(__FILE__),'location_metadata_nonce');
?>
<th scope="row" valign="top" colspan="2">
<h3><?php esc_html_e('Social Networks Options','method'); ?></h3>
</th>
<div class="form-field location-metadata">
<label for="<?php printf(esc_html__( '%s-metadata', 'method' ),$networks) ?>">
<?php printf(esc_html__('%s URL','method'),esc_html( $value )); ?>
</label>
<input type="text" name="<?php printf(esc_html__('location_%s_metadata','method'),esc_attr( $networks )) ?>"
id="<?php printf(esc_html__( '%s-metadata', 'method' ),esc_attr($networks)); ?>" value="" class="social-metadata-field">
</div>
<?php
}
这样就会在添加Location分类的时候添加一个location URL字段,并可以输入。
接下来是要保存这个字段。
保存字段的钩子是crate_{taxonomy}, 这里就是create_location, 完整的钩子就是:
add_action( 'create_location', 'save_location_social_metadata' );
创建自定义函数 save_location_social_metadata ()来实现字段的保存。
function save_location_social_metadata($term_id) { //$term_id是钩子create_location里面传递过来的,也就是要添加的分类对象的相关信息
//check if nonce set
if (!isset($_POST['location_social_nonce'])) {
// 验证是否有这个字段
return;
}
//verify nonce
if (!wp_verify_nonce( $_POST['location_social_nonce'],basename(__FILE__))) {
//验证nonce
return;
}
$social_networks = supported_social_network();
foreach($social_networks as $networks => $value) {
$term_key = sprintf('location_%s_metadata',$networks);
if (isset($_POST[$term_key])) {
update_term_meta( $term_id, esc_attr( $term_key ), esc_url_raw($_POST[$term_key]) );
//这个是主要的更新字段并且写入到数据库的函数。
}
}
}
通过以上的程序,已经成功将数据写入到字段里面。我们要进行查看或者去修改的话,要点击分类进去,也就是另外一个screen, {taxonomy}_edit_form_fields.
add_action( 'location_edit_form_fields', 'edit_location_social_metadata' );
操作和add_form_fields时候的屏幕也差不多, 就是add_foemd_fields时候是一个table header <th>管下面很多字段。 而在编辑分类界面的屏幕当中,每一条都是tabel row的格式,也就是tr > th ^ td. 也就是tr 里面,分th 和td. th 里面是label, td 是你要输入的字段数据, td里面是input.
function edit_location_social_metadata($term) {
wp_nonce_field(basename(__FILE__),'location_social_nonce');
$social_networks = supported_social_network(); ?>
<th scope="row" valign="top" colspan="2">
<h3><?php esc_html_e('Social Networks Options','method'); ?></h3>
</th>
<?php
foreach($social_networks as $networks => $value) {
$term_key = sprintf('location_%s_metadata',$networks);
$metadata = get_term_meta( $term->term_id, $term_key, true );
?>
<tr class="form-field location-metadata">
<th scope="row">
<label for="<?php printf(esc_html__( '%s-metadata', 'method' ),$networks) ?>">
<?php printf(esc_html__('%s URL','method'),esc_html( $value )); ?>
</label>
</th>
<td>
<input type="text" name="<?php printf(esc_html__('location_%s_metadata','method'),esc_attr( $networks )) ?>"
id="<?php printf(esc_html__( '%s-metadata', 'method' ),esc_attr($networks)); ?>"
value="<?php echo (!empty($metadata)) ? esc_attr( $metadata ) : ''; ?>" class="social-metadata-field">
</td>
</tr>
<?php
}
}
编辑页面的term data如何保存呢,可以和添加分类的界面共用一个保存函数, 只要是编辑分类页面创建Html函数的的nonce和添加分类界面创建html的函数nonce一样 以及input字段都是一样就可以。 只不过编辑页面保存时候函数的钩子是edit_{taxonomy}. 这里也就是edit_location.
add_action(‘edit_location’,’save_location_social_metadata’);
如果要添加图片自定义字段的话,跟添加post meta字段的图片一模一样。先要调用media upload弹出框,上传图片,保存。