Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active July 14, 2019 10:18
Show Gist options
  • Save eto4detak/84b8e867dd0a26995430272ba4be45db to your computer and use it in GitHub Desktop.
Save eto4detak/84b8e867dd0a26995430272ba4be45db to your computer and use it in GitHub Desktop.
wp js wp.media
// php --- wp_enqueue_media();
jQuery(document).ready(function($) {
frame = wp.media({
title: 'Select Image',
button: {
text: 'Select image',
},
multiple: false
});
thumbnail_id = jQuery('#_thumbnail_id')
jQuery('#clear_gallery').click(function() {
jQuery('#pixad_auto_gallery_ids').val('-1');
jQuery(this).siblings('.gallery').html('');
});
jQuery('#clear_thumbnail_id').click(function() {
thumbnail_id.val('-1');
jQuery(this).siblings('.gallery').html('');
});
jQuery('#manage_gallery').click(function() {
var gallerysc = '[gallery ids="' + jQuery('#pixad_auto_gallery_ids').val() + '"]';
wp.media.gallery.edit(gallerysc).on('update', function(g) {
var id_array = [];
$.each(g.models, function(id, img) { id_array.push(img.id); });
jQuery('#pixad_auto_gallery_ids').val(id_array.join(","));
});
});
jQuery('#manage_thumbnail_id').click(function() {
frame.on( 'select', function() {
var attachment = frame.state().get('selection').first().toJSON();
thumbnail_id.val(attachment.id);
});
frame.open();
});
// video
// jQuery('#clear_gallery_video').click(function() {
// jQuery('#pixad_auto_gallery_video').val('-1');
// jQuery(this).siblings('.wp-playlist').html('');
// });
// jQuery('#manage_gallery_video').click(function() {
// var playlistsc = '[playlist type="video" ids="' + jQuery('#pixad_auto_gallery_video').val() + '"]';
// console.log(wp);
// console.log(wp.media);
// wp.media.playlist.edit(playlistsc).on('update', function(g) {
// var id_array = [];
// jQuery.each(g.models, function(id, img) { id_array.push(img.id); });
// jQuery('#pixad_auto_gallery_video').val(id_array.join(","));
// });
// });
});
/**************************************** PHP **************************************************************/
// фильтр, показывает только свои медиа в wp.media
add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments', 10, 1 );
function show_current_user_attachments( $query = array() ) {
$user_id = get_current_user_id();
if(!current_user_can('edit_pages')){
if( $user_id ) {
$query['author'] = $user_id;
}
}
return $query;
}
///////////////////////////////////////////////////////////////////////////////////////////
/////// select image
///////////////////////////////////////////////////////////////////////////////////////////
;jQuery(document).ready(function() {
"use strict";
var customMediaLibrary = window.wp.media({
frame: 'select',
title: 'Select Images',
multiple: true,
library: {
order: 'DESC',
orderby: 'date',
type: 'image',
search: null,
uploadedTo: null // wp.media.view.settings.post.id (for current post ID)
},
button: {
text: 'Done'
}
});
$( '#manage_gallery' ).on( 'click', function( e ) {
e.preventDefault();
customMediaLibrary.open();
});
customMediaLibrary.on( 'select', function() {
// write your handling code here.
var selectedImages = customMediaLibrary.state().get( 'selection' );
console.log(selectedImages);
// Probably send the image IDs to the backend using ajax?
});
customMediaLibrary.on( 'open', function() {
console.log('test');
// Assuming the post IDs will be fetched from db.
var selectedPostIDs = [ 56,57 ];
// Used for defining the image selections in the media library.
var selectionAPI = customMediaLibrary.state().get( 'selection' );
selectedPostIDs.forEach( function( imageID ) {
/**
* This returns an attachment object based on image ID
* or creates a new one if it doesn't exist in the wp.media.Attachments.allcollection.
* Note: This does not fetch the image from db.
*/
var attachment = wp.media.attachment( imageID );
selectionAPI.add( attachment ? [ attachment ] : []);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment