通常,要将字段添加到快速编辑字段,我们应该使用\'quick_edit_custom_box\'
仅为自定义列触发的操作挂钩,因为核心列被显式排除(see code).
如果我们添加一个自定义列,那么它将显示在列表中,但这没有意义,因为列描述已经存在。
但是,我们可以使用两个技巧添加一个不可见列:
将其标签设置为空字符串(这样它就不会显示在“屏幕选项”设置中)
强制隐藏列get_user_option_manageedit-{$taxonomy}columnshidden
过滤器挂钩首先创建不可见列:/*
* This is NOT required, but I\'m using it to easily let you customize the taxonomy
* where to add the inline description.
* You can replace $the_target_tax in all the code with the name of your taxonomy,
* with no need to use the global variable.
*/
global $the_target_tax;
$the_target_tax = \'category\';
add_filter( "manage_edit-{$the_target_tax}_columns", function( $columns ) {
$columns[\'_description\'] = \'\';
return $columns;
});
add_filter( "manage_{$the_target_tax}_custom_column", function( $e, $column, $term_id ) {
if ( $column === \'_description\' ) return \'\';
}, 10, 3 );
add_filter( "get_user_option_manageedit-{$the_target_tax}columnshidden", function( $r ) {
$r[] = \'_description\';
return $r;
});
现在我们有了一个自定义列\'_description\'
这是不可见的,但可用于通过\'quick_edit_custom_box\'
挂钩:但是,这个钩子不会传递任何当前值来用当前描述预填充字段,但我们可以使用jQuery的几行代码来完成这一操作:
add_action( \'quick_edit_custom_box\', function( $column, $screen, $tax ) {
if ( $screen !== \'edit-tags\' ) return;
$taxonomy = get_taxonomy( $tax );
if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) return;
global $the_target_tax;
if ( $tax !== $the_target_tax || $column !== \'_description\' ) return;
?>
<fieldset>
<div class="inline-edit-col">
<label>
<span class="title"><?php _e(\'Description\'); ?></span>
<span class="input-text-wrap">
<textarea id="inline-desc" name="description" rows="3" class="ptitle"></textarea>
</span>
</label>
</div>
</fieldset>
<script>
jQuery(\'#the-list\').on(\'click\', \'a.editinline\', function(){
var now = jQuery(this).closest(\'tr\').find(\'td.column-description\').text();
jQuery(\'#inline-desc\').text( now );
});
</script>
<?php
}, 10, 3 );
现在我们有了表单,我们需要在提交时保存数据,这非常容易使用"edited_{$taxonomy}"
挂钩:function save_inline_description( $term_id ) {
global $the_target_tax;
$tax = get_taxonomy( $the_target_tax );
if (
current_filter() === "edited_{$the_target_tax}"
&& current_user_can( $tax->cap->edit_terms )
) {
$description = filter_input( INPUT_POST, \'description\', FILTER_SANITIZE_STRING );
// removing action to avoid recursion
remove_action( current_filter(), __FUNCTION__ );
wp_update_term( $term_id, $the_target_tax, array( \'description\' => $description ) );
}
}
add_action( "edited_{$the_target_tax}", \'save_inline_description\' );
代码经过了快速测试,似乎正常工作,请注意,它需要PHP 5.3+。