Proper way to add a button to the top menu of the media library?

So, this is the only way I’ve found so far and it’s ugly, however WordPress offers no means of modifying this through filters that I can find in core. I present, the hacky “do-it-through-Javascript” method:

add_action( 'load-upload.php', function(){
    $js = <<<JAVASCRIPT
jQuery(document).ready(function($){
    setTimeout(function() {
        $('.wp-filter .button:last' ).after($('<button class="button">Hi</button>'));
    }, 1000);
});
JAVASCRIPT;

    add_filter( 'admin_footer', function() use ($js) {
        echo "<script>{$js}</script>";
    });
});

Obviously, you should be enqueuing the Javascript you want instead of doing it like I am, but this is a proof-of-concept. It will add a button to both Media Library toolbars (list view and gallery view).

The reason for the setTimeout is that the filter bars actually load after page load, so waiting on document ready is not enough. There’s probably a better place to hook this in, but this should be a good start.

Leave a Comment