4

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 and 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?

5

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.

  • I was just looking into post_tags_meta_box to figure something like this. Can we filter every function in the core? Even if it's not being filtered already? – Jack Johansson Jun 22 '17 at 16:18
  • I guess there are different ways around this, so please share what you come up with. I wasn't sure if I should post this on the other linked question, but that question sounded more general for all metaboxes. So I posted it here instead. In theory one could use output buffering on the whole page and replace anything we want, but that's totally over the top ;-) Sometimes it's better to limit the scope a little bit more than that and use hooks/api if they are available for the job. – birgire Jun 22 '17 at 16:24
  • Well i was just wondering if we can directly filter the post_tags_meta_box( $post, $box ); and pass our own arguments. Is it possible? – Jack Johansson Jun 22 '17 at 16:26
  • I didn't find any explicit filters for post_tags_meta_box() and it uses echo's instead of return,. So I Ijust registered a wrapper instead, with modifications, that also calls the original post_tags_meta_box() callback. – birgire Jun 22 '17 at 16:31
  • Yes I heard in the chatroom that the function is going to be merged with category list. Thanks! – Jack Johansson Jun 22 '17 at 16:42

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.