1

Taxonomy description

I want to add some helpful text to the edit-tags screen describing the proper use of each of my custom taxonomies.

I see in the Codex that a description can be added to a custom taxonomy — not to a term, but to the taxonomy itself. That would be a perfect place to put my help text, and I have, but I don't see where that's rendered at all.

From the Codex:

description (string) (optional) Include a description of the taxonomy. Default: ""

In my custom taxonomy function's $args array:

$args = array( 'description' => 'Some helpful text!' [other args...] );

Is there a hook for edit-tags that I can use to display the taxonomy's description, or another solution (ACF maybe) for inserting help text here?

2 Answers 2

1

The code for where you've circled can be found in wp-admin/edit-tags.php:295

You'll notice there's nothing there. No hooks, no filters. You're out of luck for tapping into that cleanly.

Luckily you can do a duct tape method to still add it with jQuery. You can dynamically put text where you've circled by doing something like:

add_action( 'admin_head', function(){
    global $wp_query;
    $screen = get_current_screen();
    if ($screen->base == 'edit-tags' || $screen->base == 'term') {
        $mytax = get_taxonomy($screen->taxonomy);
        if (!empty($mytax->description)) {
            ?>
            <script>
            jQuery(window).load(function() {
                jQuery('.wrap h1').after("<p class='description'><?php echo $mytax->description ?></p>");
            });
            </script>
            <?php
        }
    }
});

UPDATE

As you pointed out @Slam, you can use the _pre_add_form and _term_edit_form_top hooks to show the into around the area you're after. To do that, you can cycle through all the taxonomies and dynamically run the actions like so:

add_action( 'admin_init', function(){
    $taxonomies = get_taxonomies(); 
    foreach ( $taxonomies as $taxonomy ) {
        add_action("{$taxonomy}_pre_add_form", 'my_plugin_tax_description');
        add_action("{$taxonomy}_term_edit_form_top", 'my_plugin_tax_description');
    }
});

function my_plugin_tax_description() {
    global $wp_query;
    $screen = get_current_screen();
    if ($screen->base == 'edit-tags' || $screen->base == 'term') {
        $mytax = get_taxonomy($screen->taxonomy);
        if (!empty($mytax->description))
            echo "<p class='description'>{$mytax->description}</p>";
    }
}

Although _pre_add_form fires in the left column - not directly below the h1 title.

2
  • 1
    I'm don't full understand hooks yet, but what about taxonomy_pre_add_form?
    – Slam
    Jan 4, 2018 at 2:37
  • you're absolutely right, I'm embarrassed I didn't see that. I've updated my original post. Jan 4, 2018 at 4:00
0

To @David Sword comments: If you want use standard taxonomy description (in the labels) use this code:

function my_plugin_tax_description() {
    global $wp_query;
    $screen = get_current_screen();
    if ($screen->base == 'edit-tags' || $screen->base == 'term') {
        $mytax = get_taxonomy($screen->taxonomy);
        if (!empty($mytax->labels->description))
            echo "<p class='description'>{$mytax->labels->description}</p>";
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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