Wordpress - Add 'Description' to taxonomy 'Quick Edit'
Normally to add fields to the quick edit field, we should use 'quick_edit_custom_box'
action hook that is triggered only for custom columns, because core columns are explicitly excluded (see code).
If we add a custom column, then it will be shown in the list table, but it doesn't make sense, because the column description is already present.
However, we have the possibility to add an invisible column using 2 tricks:
- Setting its label to an empty string (in this way it is not shown in the "Screen Option" settings)
- Force the column to be hidden acting on
get_user_option_manageedit-{$taxonomy}columnshidden
filter hook
First create the invisible column:
/*
* 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;
});
Now we have a custom column '_description'
that is invisible, but can be used to put additional fields via 'quick_edit_custom_box'
hook:
However, this hook does not pass any current value to pre-fill the field with the current description, but we can use a few lines of jQuery to do that:
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 );
Now that we have the form, we need to save data when submitted, pretty easy using "edited_{$taxonomy}"
hook:
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' );
Code was quickly tested and seems to work, note that it requires PHP 5.3+.