When inserting media into a post, is there a way to change the default-view of the Media-Library from “All media items” to “Uploaded to this post”?
There is another thread where this question was extracted from:
How to manage attachment relationships
There were two minor mistakes in my previous answer:
change
event for the parent.Here is the fixed code:
The only problem with the JS above that it toggles the select box to trigger the change after the page loads and after its already begun to download ALL MEDIA ITEMS. For my client on a slow T1 this locked things up as it downloaded both the ALL MEDIA TIMES and the UPLOADED TO THIS POST items together.
I had some help from the great Sewpafly who develops Post Thumbnail Editor Plugin. he shared a great piece of JS that prevents the load of ALL MEDIA ITEMS and forces it to load only images “UPLOADED TO THIS POST” by default.
The Code
File: myadmin.js
File: functions.php
The same code on GitHub: https://gist.github.com/fishnyc22/5593693
I dropped that into a JS file and called it in functions.php with the admin_enqueue_scripts. See GIST above for both the PHP and JS.
Works brilliantly. Hopefully the fine wordpress folks fix this in a upcoming update, but for now Sewpafly has the best solution I’ve found. Thanks again buddy.
I should note that I have just discovered that the viewer defaults to MEDIUM sized images which I had disabled (set to 0,0) since I was not using and preventing bloat. When medium size isn’t available wordpress loads the FULL size image. I have since given in enabled the medium size.
@toscho Ah, I found a bug in your code. Please bare with me. Do the exact following to replicate the issue:
1) Open a draft post.
2) Click on
Add Media
button. Wait for the jQuery function to load.3) On your left, click on the
Set Featured Image
link.4) Now close the Media popup window and on the post edit page, click on the
Set featured image
link on the right side-bar.5) You will see that the jQuery function will not work.
However, if you would have clicked on the
Set featured image
link first on post edit load, the function would work. Can you replicate this issue and possibly find a solution? Sorry again for posting this as an answer but this platform does not offer me a better option currently.EDIT: Can someone please let toscho know about this. You can do this by adding a comment to his answer which I believe should give him a notification. I cannot write comments as I do not have enough reputation…
EDIT 2: If you want to desperately avoid this issue, you can remove the “Set Featured Image” link in the popup and force the user to use the sidebar link (like WP versions prior to 3.5). Use this filter that was introduced in WP 3.5:
As I said, this is a desperate fix until there is a solution posted for the main code.
The main thing I didn’t like about most of these solutions is that it was blinking on alot of screens, and if you clicked on Featured Image it would revert back to showing all images. After some searching I think found a permanent solution (Thanks to Ãnsal Korkmaz) which looks like it solves these problems. The code:
which permanently sets the media library to only show “Uploaded To This Post”, even if you try and change the view which means no blinking.
I’ve added this code to accompany it to remove the selectbox entirely:
The new versions of wordpress use a different approach (advanced Backbonejs) for the media uploader;
Check the code from this plugin as it works for new versions of WordPress.
This is my solutions to set
dateFilter
to current month, though it triggers AJAX twice.