7

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.

Thanks everyone!

  • 1
    I spent some time digging around in the core files looking for undocumented actions and filters that you might hook into in upload.php or the associated classes that it relies upon, but I'm not finding anything. I think the answer is going to be "JavaScript away!" Sorry. – Scott Nelle Jul 30 '14 at 21:01
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.

| improve this answer | |

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.