I’ve been searching around for a way to add a message to the tag metabox before the input data-wp-taxonomy. When searching under the tags tags and metabox but I was only able to populate fourteen results.

When I research the core for data-wp-taxonomy I found the meta-boxes.php file and the function for post_tags_meta_box which led me to How to Add Reminders/Notes to New Post Meta Boxes but that question is over five years old and ties into it with jQuery:

function load_my_alerts(){
      wp_register_script( 
        'my_alerts', 
        get_template_directory_uri() . '/js/alerts.js', 
        array( 'jquery' )
    );
    wp_enqueue_script( 'my_alerts' );
}
add_action('admin_enqueue_scripts', 'load_my_alerts');

Is there another way to add a message before the input on the tag metabox?

1 Answer
1

Here’s a workaround specific for the post tags meta box.

We can register a custom metabox callback for the post_tag taxonomy with:

add_filter( 'register_taxonomy_args', function( $args, $taxonomy )
{
    // Replace the original (post_tag) metabox callback with our wrapper
    if( 'post_tag' === $taxonomy )
        $args['meta_box_cb'] = 'wpse_post_tags_meta_box';

    return $args;

}, 10, 2 );

where our custom callback is e.g.:

function wpse_post_tags_meta_box( $post, $box )
{
    // Custom action
    do_action( 'wpse_before_post_tags_meta_box', $post, $box );

    // Original callback. Note it will echo the stuff, not return it
    post_tags_meta_box( $post, $box );
}

Now we can hook into the custom wpse_before_post_tags_meta_box hook when needed.

If we need to inject something inside the post_tags_meta_box() function, then we might try using output buffering to work with it as a string. It’s also possible to duplicate that function, but that function could easily change in the future! So I would avoid that if possible.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *