First, see screenshot to see what I’m trying to select:
In WordPress, when I click New Post, then the Add Media button, on the popup image gallery there’s the image filtering drop-down menu; I want to automatically select. the “Unattached” item when that popup loads.
I’m having trouble getting this to happen. So far, in my theme’s functions.php
I have a call to the .js file with the jQuery code in it.
function my_custom_scripts() {
wp_enqueue_script( 'my-custom', THEMATER_URL . '/js/custom.js', array('jquery'), '1.0.0', true );
}
add_action('wp_enqueue_scripts', 'my_custom_scripts');
Then in the .js file I have:
jQuery(document).ready(function($) {
$('#media-attachment-filters').prop('selectedIndex', 6); // select 5th option
});
But nothing happens. What am I missing?
Update:
Based on Christian Nguyen’s code, I did get it to select the item I wanted, however, it’s not actually sorting the image gallery based on that selection, so it’s functionally just ‘highlighting’ the item. My guess is it’s not triggering some OnClick
/OnSelect
event. I tried adding .trigger('change')
, but still no joy. Does anyone also know how to get it to actually do so?
So far, I have this code, note that ALL of the commented out items WILL select the item, just no actual “selection” triggered to do the sorting.
/* When Insert Media button gets clicked ... */
$("#insert-media-button").on('click', function() {
// Set a 'delay' of 0, & then call the function that selects the menu item.
// NOTE: Skipping the setTimeout, & just directly call the selector doesn't work.
setTimeout(function() {
/* BUG: All 4 of the following WILL select the "Unattached" drop-down item,
However, ALL pics still get loaded, instead of JUST the UNATTACHED
pics, which is what we want.
*/
//$('#media-attachment-filters').attr('selected', 'selected').trigger('change');
//$('#media-attachment-filters').val('unattached');
//$('#media-attachment-filters').prop('selectedIndex', 5);
//$('#media-attachment-filters>option:eq(5)').prop('selected', true);
}, 0);
});
Another thing of NOTE: in my case, the .js also wasn’t being called at all in the admin area because I mistakenly only enqueued it for the front-end, I had to use admin_enqueue_scripts
to get the .js to load in the admin area (back end) where the action is actually happening.
I have a temporary solution:
You should be able to do this with the
.val()
function if you have an associatedvalue
on your<option>
elements. An example may include…JSFiddle Link
In this question you can find 2 solutions to do what you want and a tip to remove the list options that you doesn’t want.
The firsts works on
admin_footer-post-new.php
andadmin_footer-post.php
hook action and trigger the change event to the right options inside jqueryajaxStop()
function on$('#wpcontent')
. This solutions load all the images, so the first time is slow than the other.The seconds on
admin_enqueue_scripts
hook action, the same jquery event but usewp.media.view.MediaFrame.Post
to prevent to load all the image.This solutions is faster but have some that it’s easy to workaround.
OK, looks like I’ve got it figured out; it needed
.change()
appended to the end, and I’ve also increased thesetTimeout()
from0
to10
. Depending on how fast/slow your own server is, you may want to play around with increasing/decreasing that value. Essentially it’s meant to give the initial image loading a bit of time to ‘load’ before calling the jQuery.Here’s my final working code. Thanks again to Christian Nguyen for 99% of the code: