pre_delete_term - 在删除术语时清理数据无论何时你看到{$taxonomy} 你可以用你的分类slug来代替它。在本例中,我将使用默认/内置的帖子类别分类法,其slug为category
.下面的will not 包括添加要快速编辑的字段或添加新术语元数据库。
让我们从显示文本框开始接受类。由于两个页面的数据显示方式不同(并传递单独的参数),因此我们必须创建两个单独的函数并将其分配给{$taxonomy}_add_form_fields
和{$taxonomy}_edit_form_fields
分别地这两篇评论都指出了我们的任何与众不同之处。
/**
* Category "Add New" Page - Add Additional Field(s)
* @param string $taxonomy
*/
function category_fields_new( $taxonomy ) { // Function has one field to pass - Taxonomy ( Category in this case )
wp_nonce_field( \'category_meta_new\', \'category_meta_new_nonce\' ); // Create a Nonce so that we can verify the integrity of our data
?>
<div class="form-field">
<label for="category_fa">Font-Awesome Icon</label>
<input name="category_icon" id="category_fa" type="text" value="" style="width:100%" />
<p class="description">Enter a custom font-awesome icon - <a href="http://fontawesome.io/icons/" target="_blank">List of Icons</a></p>
</div>
<?php
}
add_action( \'category_add_form_fields\', \'category_fields_new\', 10 );
/**
* Category "Edit Term" Page - Add Additional Field(s)
* @param Object $term
* @param string $taxonomy
*/
function category_fields_edit( $term, $taxonomy ) { // Function has one field to pass - Term ( term object) and Taxonomy ( Category in this case )
wp_nonce_field( \'category_meta_edit\', \'category_meta_edit_nonce\' ); // Create a Nonce so that we can verify the integrity of our data
$category_icon = get_option( "{$taxonomy}_{$term->term_id}_icon" ); // Get the icon if one is set already
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="category_fa">Font-Awesome Icon</label>
</th>
<td>
<input name="category_icon" id="category_fa" type="text" value="<?php echo ( ! empty( $category_icon ) ) ? $category_icon : \'\'; ?>" style="width:100%;" /> <!-- IF `$category_icon` is not empty, display it. Otherwise display an empty string -->
<p class="description">Enter a custom Font-Awesome icon - <a href="http://fontawesome.io/icons/" target="_blank">List of Icons</a></p>
</td>
</tr>
<?php
}
add_action( \'category_edit_form_fields\', \'category_fields_edit\', 10, 2 );
下一步是保存我们的选项。由于类别创建和类别编辑在保存时的处理方式相同,因此我们将对两者使用相同的功能。我们仍然需要使用created_{$taxonomy}
和edited_{$taxonomy}
. 如上所述,由于分类元数据不存在,我们必须将数据保存到需要唯一名称的选项表中。这就是为什么我们给我们的选项一个唯一的名称{$taxonomy}_{$term_id}_icon
在本例中,转换为category_1_icon
./**
* Save our Additional Taxonomy Fields
* @param int $term_id
*/
function save_category_fields( $term_id ) {
/** Verify we\'re either on the New Category page or Edit Category page using Nonces **/
/** Verify that a Taxonomy is set **/
if( isset( $_POST[\'taxonomy\'] ) && isset( $_POST[\'category_icon\'] ) &&
(
isset( $_POST[\'category_meta_new_nonce\'] ) && wp_verify_nonce( $_POST[\'category_meta_new_nonce\'], \'category_meta_new\' ) || // Verify our New Term Nonce
isset( $_POST[\'category_meta_edit_nonce\'] ) && wp_verify_nonce( $_POST[\'category_meta_edit_nonce\'], \'category_meta_edit\' ) // Verify our Edited Term Nonce
)
) {
$taxonomy = $_POST[\'taxonomy\'];
$category_icon = get_option( "{$taxonomy}_{$term_id}_icon" ); // Grab our icon if one exists
if( ! empty( $_POST[\'category_icon\'] ) ) { // IF the user has entered text, update our field.
update_option( "{$taxonomy}_{$term_id}_icon", htmlspecialchars( sanitize_text_field( $_POST[\'category_icon\'] ) ) ); // Sanitize our data before adding to the database
} elseif( ! empty( $category_icon ) ) { // Category Icon IS empty but the option is set, they may not want an icon on this category
delete_option( "{$taxonomy}_{$term_id}_icon" ); // Delete our option
}
} // Nonce Conditional
} // End Function
add_action ( \'created_category\', \'save_category_fields\' );
add_action ( \'edited_category\', \'save_category_fields\' );
最后,我们需要清理我们的选择。删除帖子时,Posteta会删除自身,但选项不会删除。我们需要手动删除这些选项,以便我们的选项表不会因孤立选项而膨胀。/**
* Essential Cleanup of our Options
* @param int $term_id
* @param string $taxonomy
*/
function remove_term_options( $term_id, $taxonomy ) {
delete_option( "{$taxonomy}_{$term_id}_icon" ); // Delete our option
}
add_action( \'pre_delete_term\', \'remove_term_options\', 10, 2 );
如果我们想显示当前分配的每个图标。由于我们使用分类法和术语ID生成唯一的选项名称,因此我们将使用相同的公式获得选项。首先让我们了解我们的条款,然后让我们循环了解我们的条款。$terms = get_terms( // Returns an array of Term Objects,
\'category\', // Pass our Taxonomy
array( // Pass our Arguments
\'hide_empty\' => false // Show ALL Terms
)
);
if( ! empty( $terms ) ) { // Make sure we have some terms to loop through
foreach( $terms as $term ) { // Loop through each term
$icon = get_option( "{$term->taxonomy}_{$term->term_id}_icon" ); // Use our formula to grab the Icon - This translate to "category_1_icon"
echo "Category {$term->name} Icon: {$icon}<br />"; // Display our Category Icon
}
}