1

The "Add Media" button allows any media to be added to current editor. I need the "Uploaded to this page" from the dropdown menu to be the only option available. Some time ago I found a solution somewhere like this:

jQuery(function($){
    $(document).on("DOMNodeInserted", function(){
        // Lock uploads to "Uploaded to this post"
        $('select.attachment-filters [value!="uploaded"]').remove()
        $('select.attachment-filters').trigger('change');
    });
})

However, this event conflicts with my other plugin because that plugin will insert more than 100 nodes and this code snippet will slow that process down very much: it takes 2 minutes longer to load with this event attached.

Therefore, I am looking for a better solution that will save the time and browser's resource here. Any suggestion is welcome!

1

Maybe I was over thinking, but the solution is easier than I thought.

I just have to limit the event handler to some element, so other unrelated ones will not trigger the handler. In this case, .media-modal is the overall parent of the media library element, so I am specifying this extra selector to the event, like below:

jQuery(document).on("DOMNodeInserted", '.media-modal', function() {
    // ...
}

And after testing, this will speed up the other insertion of nodes.

0

This is what I've found which I use regularly. Really it's the same thing you're using but placing it into the admin footer so that it's one of the last things to load. Maybe it's faster?

function media_library_filter(){ 
    ?>
      <script type="text/javascript">
        jQuery(document).on("DOMNodeInserted", function(){
            jQuery('select.attachment-filters [value="uploaded"]').attr( 'selected', true ).parent().trigger('change');
        });
      </script>
    <?php 
}
add_action( 'admin_footer-post-new.php', 'media_library_filter' );
add_action( 'admin_footer-post.php', 'media_library_filter' );

You can view this but it's kind of old:

Change the default-view of Media Library in 3.5?

  • Nice idea but those 100 nodes are inserted when a certain button is clicked, so we don't know when they will be inserted. – karbuncle Aug 26 '14 at 20:53

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.