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

I want to add a simple button to the Media Library top menu (say, to the right of the bulk actions / filters dropdowns. I have scoured the WordPress codex for a proper way to do this and have found nothing useful.

So before I start attacking this problem with Javascript, I wanted to ask the community if there’s a proper WordPress API way of doing this – along the lines of
manage_media_columns and manage_media_custom_column.

Read More

Thanks everyone!

Related posts

1 comment

  1. 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.

Comments are closed.