7

This seems fairly straight forward, but I cannot find a definite guide on how to add the taxonomy description field to the quick edit box.

I've followed several tutorials on adding fields to the custom quick edit box, but the core fields (name, description and slug) do not follow the same guidelines as adding custom fields to the taxonomy quick edit box.

Is it even possible to do so? If not - I guess the best alternate is to create another 'Description' meta-field and that way add it to the taxonomy quick edit box.

15

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:

  1. Setting its label to an empty string (in this way it is not shown in the "Screen Option" settings)
  2. 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+.

Add description to taxonomy quick edit

  • 1
    Hey G.M.! Thank you for the explanation. This is awesome and I would not haven been able to do this if it wasn't for your help. Much Appreciated. When the 'description' is saved in the Quick Edit box, if you access the quick edit again - the old text still displays and the custom js I written no longer runs. I know this is due to the AJAX, but I'm unsure how to reload the script inside of the 2nd function. Any tip on how to reload the custom script? THANK YOU again btw. – Roc Apr 3 '14 at 0:58
  • @Roc The problem was that WordPress clone via javascript the edit form, so when I set the current value to the textarea, the form I was looking for does not exist anymore.. So, I have to run the action not only on document ready, but on every click, using jQuery.on() to make it work with ajax. Edited the answer (only the javascript), now it works. – gmazzap Apr 3 '14 at 13:41
0

Just wanted to point out that i was not able to get the code to work without changing the 'category' parameter to my custom taxonomy name.

For example:

wp_update_term( $term_id, 'category', array( 'description' => $description ) );

was replaced with:

wp_update_term( $term_id, 'my_custom_tax_name', array( 'description' => $description ) );

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.