Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Andy Mercer on "Call Media Editor Gallery Page Directly"

$
0
0

I'm calling the WP 3.5 media uploader from a metabox, and have gotten it working to select single or multiple images. What I'd really like to do is call it to the Gallery's page instead though, so that I can then click through to the sorting page, and be able to sort the images.

My current code is this:

jQuery('#fg_select').on('click', function(event){

		event.preventDefault();

		// If the media frame already exists, reopen it.
		if ( file_frame ) {
			file_frame.open();
			return;
		}

		// Create the media frame.
		file_frame = wp.media.frame = wp.media({
			title: "Select Images For Gallery",
			button: {text: "Select",},
			library : { type : 'image'},
			multiple: true // Set to true to allow multiple files to be selected
		});

		file_frame.on('open', function() {
			var selection = file_frame.state().get('selection');
			ids = jQuery('#fg_metadata').val().split(',');
			ids.forEach(function(id) {
				attachment = wp.media.attachment(id);
				attachment.fetch();
				selection.add( attachment ? [ attachment ] : [] );
			});
		});

		file_frame.on('ready', function() {
			// Here we can add a custom class to our media modal.
			// .media-modal doesn't exists before the frame is
			// completly initialised.
			$( '.media-modal' ).addClass( 'no-sidebar' );
		});

		// When an image is selected, run a callback.
		file_frame.on('select', function() {
			var imageIDArray = [];
			var imageHTML = '';
			var metadataString = '';
			images = file_frame.state().get('selection');
			images.each(function(image) {
				imageIDArray.push(image.attributes.id);
				imageHTML += '
<li><button></button><img id="'+image.attributes.id+'" src="'+image.attributes.url+'"></li>
';
			});
			metadataString = imageIDArray.join(",");
			if(metadataString){
				jQuery("#fg_metadata").val(metadataString);
				jQuery("#featuredgallerydiv ul").html(imageHTML);
				jQuery('#fg_select').text('Edit Selection');
				jQuery('#fg_removeall').addClass('visible');
			}
		});

		// Finally, open the modal
		file_frame.open();

	});

[Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

Any ideas?


Viewing all articles
Browse latest Browse all 8245

Trending Articles