WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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

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!

share|improve this question

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.

share|improve this answer

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');
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?

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.