php – 从Metabox调用WordPress Gallery Uploader / Selector

前端之家收集整理的这篇文章主要介绍了php – 从Metabox调用WordPress Gallery Uploader / Selector前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我单击帖子/页面上的添加媒体按钮时,我可以选择添加媒体.选择媒体后,单击“插入到帖子”,然后插入图像.但是,还有另一个选项,位于左侧边栏.我可以点击创建图库.图像选择过程是相同的,但是当我单击“创建新图库”时,它会转到一个新框架,允许我编辑图像的顺序.

第二个窗口就是我追求的目标.我从MetaBox调用框架,我已经成功地让它抓住单个或多个图像并将ID保存为字符串,以及将缩略图直接插入预览框.我找不到任何关于调用Gallery框架的信息.

我目前的代码如下:

  1. jQuery('#fg_select').on('click',function(event){
  2.  
  3. event.preventDefault();
  4.  
  5. // If the media frame already exists,reopen it.
  6. if ( file_frame ) {
  7. file_frame.open();
  8. return;
  9. }
  10.  
  11. // Create the media frame.
  12. file_frame = wp.media.frame = wp.media({
  13. title: "Select Images For Gallery",button: {text: "Select",},library : { type : 'image'},multiple: true // Set to true to allow multiple files to be selected
  14. });
  15.  
  16. file_frame.on('open',function() {
  17. var selection = file_frame.state().get('selection');
  18. ids = jQuery('#fg_Metadata').val().split(',');
  19. ids.forEach(function(id) {
  20. attachment = wp.media.attachment(id);
  21. attachment.fetch();
  22. selection.add( attachment ? [ attachment ] : [] );
  23. });
  24. });
  25.  
  26. file_frame.on('ready',function() {
  27. // Here we can add a custom class to our media modal.
  28. // .media-modal doesn't exists before the frame is
  29. // completly initialised.
  30. $( '.media-modal' ).addClass( 'no-sidebar' );
  31. });
  32.  
  33. // When an image is selected,run a callback.
  34. file_frame.on('select',function() {
  35. var imageIDArray = [];
  36. var imageHTML = '';
  37. var MetadataString = '';
  38. images = file_frame.state().get('selection');
  39. images.each(function(image) {
  40. imageIDArray.push(image.attributes.id);
  41. imageHTML += '<li><button></button><img id="'+image.attributes.id+'" src="'+image.attributes.url+'"></li>';
  42. });
  43. MetadataString = imageIDArray.join(",");
  44. if(MetadataString){
  45. jQuery("#fg_Metadata").val(MetadataString);
  46. jQuery("#featuredgallerydiv ul").html(imageHTML);
  47. jQuery('#fg_select').text('Edit Selection');
  48. jQuery('#fg_removeall').addClass('visible');
  49. }
  50. });
  51.  
  52. // Finally,open the modal
  53. file_frame.open();
  54.  
  55. });

有任何想法吗?

编辑:

我已经把它直接调用了画廊,没有任何侧边栏等等.但是,它现在忽略了on(‘select’)调用.我猜画廊在选择图像时会发出不同的电话?

  1. jQuery(document).ready(function($){
  2.  
  3. // Uploading files
  4. var file_frame;
  5.  
  6. jQuery('#fg_select').on('click',reopen it.
  7. if ( file_frame ) {
  8. file_frame.open();
  9. return;
  10. }
  11.  
  12. // Create the media frame.
  13. file_frame = wp.media.frame = wp.media({
  14. frame: "post",state: "featured-gallery",button: {text: "Edit Image Order"},multiple: true
  15. });
  16.  
  17. file_frame.states.add([
  18. new wp.media.controller.Library({
  19. id: 'featured-gallery',title: 'Select Images for Gallery',priority: 20,toolbar: 'main-gallery',filterable: 'uploaded',library: wp.media.query( file_frame.options.library ),multiple: file_frame.options.multiple ? 'reset' : false,editable: true,allowLocalEdits: true,displaySettings: true,displayUserSettings: true
  20. }),]);
  21.  
  22. file_frame.on('open',');
  23. if (!empty(ids)) {
  24. ids.forEach(function(id) {
  25. attachment = wp.media.attachment(id);
  26. attachment.fetch();
  27. selection.add( attachment ? [ attachment ] : [] );
  28. });
  29. }
  30. });
  31.  
  32. file_frame.on('ready',function() {
  33. // Here we can add a custom class to our media modal.
  34. // .media-modal doesn't exists before the frame is
  35. // completly initialised.
  36. $( '.media-modal' ).addClass( 'no-sidebar' );
  37. });
  38.  
  39. file_frame.on('change',function() {
  40. // Here we can add a custom class to our media modal.
  41. // .media-modal doesn't exists before the frame is
  42. // completly initialised.
  43. setTimeout(function(){
  44. $('.media-menu a:first-child').text('← Edit Selection').addClass('button').addClass('button-large').addClass('button-primary');
  45. },0);
  46. });
  47.  
  48. // When an image is selected,run a callback.
  49. file_frame.on('set',function() {
  50. alert('test');
  51. });
  52.  
  53. // Finally,open the modal
  54. file_frame.open();
  55.  
  56. });

编辑2:

好的,所以我已经把所有东西都正确地解雇了.但我无法破译输出的图库代码.

  1. // When an image is selected,run a callback.
  2. file_frame.on('update',");
  3. if (MetadataString) {
  4. jQuery("#fg_Metadata").val(MetadataString);
  5. jQuery("#featuredgallerydiv ul").html(imageHTML);
  6. jQuery('#fg_select').text('Edit Selection');
  7. jQuery('#fg_removeall').addClass('visible');
  8. }
  9. });

$imageArray或$imageHTML没有任何结果. $image是东西,它是[对象对象].

编辑3:如下面评论中所述,编辑2中的代码的主要问题是,当使用Gallery时,您必须调用“库”而不是“选择”.

  1. // Uploading files
  2. var file_frame;
  3.  
  4. jQuery('#fg_select').on('click',state: "gallery",multiple: true
  5. });
  6.  
  7. file_frame.on('open',function() {
  8. var selection = file_frame.state().get('selection');
  9. var ids = jQuery('#fg_Metadata').val();
  10. if (ids) {
  11. idsArray = ids.split(',');
  12. idsArray.forEach(function(id) {
  13. attachment = wp.media.attachment(id);
  14. attachment.fetch();
  15. selection.add( attachment ? [ attachment ] : [] );
  16. });
  17. }
  18. });
  19.  
  20. // When an image is selected,function() {
  21. var imageIDArray = [];
  22. var imageHTML = '';
  23. var MetadataString = '';
  24. images = file_frame.state().get('library');
  25. images.each(function(attachment) {
  26. imageIDArray.push(attachment.attributes.id);
  27. imageHTML += '<li><button></button><img id="'+attachment.attributes.id+'" src="'+attachment.attributes.url+'"></li>';
  28. });
  29. MetadataString = imageIDArray.join(",");
  30. if (MetadataString) {
  31. jQuery("#fg_Metadata").val(MetadataString);
  32. jQuery("#featuredgallerydiv ul").html(imageHTML);
  33. jQuery('#fg_select').text('Edit Selection');
  34. jQuery('#fg_removeall').addClass('visible');
  35. }
  36. });
  37.  
  38. // Finally,open the modal
  39. file_frame.open();
  40.  
  41. });

我现在遇到的主要问题是我无法通过选择打开图库编辑.我可以把它打开,但没有选择图像.我在调查那个.我也在考虑重新开放而不是创建新视图并发送预选.如果我进入选择窗口,然后是订单窗口,但是单击X关闭,我可以重新打开订单窗口.所以应该有办法.

编辑4

根据下面的答案中的代码,我已将预选代码更改为:

  1. file_frame.on('open',function() {
  2. var library = file_frame.state().get('library');
  3. var ids = jQuery('#fg_perm_Metadata').val();
  4. if (ids) {
  5. idsArray = ids.split(',');
  6. idsArray.forEach(function(id) {
  7. attachment = wp.media.attachment(id);
  8. attachment.fetch();
  9. library.add( attachment ? [ attachment ] : [] );
  10. });
  11. }
  12. });

这允许我直接重新打开图库编辑状态并预先选择图像.但是,当我直接打开这个状态时,我无法点击取消图库(返回图像选择状态).单击该按钮/链接只会关闭框架.我尝试预填充库和选择,但这也不起作用.以下是来自media-views.js,似乎是控制该按钮的内容.它不是将状态更改为特定状态,而是将其更改为先前的状态.由于我们直接打开gallery-edit,因此没有过去的状态.我想知道是否可以打开图库,然后打开,更改为图库编辑.立即执行以便用户看不到,但是它会将过去的状态带入系统.

  1. galleryMenu: function( view ) {
  2. var lastState = this.lastState(),prevIoUs = lastState && lastState.id,frame = this;

编辑5:

终于想通了.我根本无法完成上述任务,我不知道为什么.因此,可能有更好的方法来执行此操作,涉及该代码.如果是这样,我很想知道.

  1. file_frame.on('open',function() {
  2. var selection = file_frame.state().get('selection');
  3. var library = file_frame.state('gallery-edit').get('library');
  4. var ids = jQuery('#fg_perm_Metadata').val();
  5. if (ids) {
  6. idsArray = ids.split(',');
  7. idsArray.forEach(function(id) {
  8. attachment = wp.media.attachment(id);
  9. attachment.fetch();
  10. selection.add( attachment ? [ attachment ] : [] );
  11. });
  12. file_frame.setState('gallery-edit');
  13. idsArray.forEach(function(id) {
  14. attachment = wp.media.attachment(id);
  15. attachment.fetch();
  16. library.add( attachment ? [ attachment ] : [] );
  17. });
  18. }
  19. });

最终编辑

我的代码现在完全正常工作,我很感激帮助!如果您想看到它的实际效果,请查看http://wordpress.org/plugins/featured-galleries/

我对WP比较陌生.事实上,我正在构建我的第一个WP主题,我和你一样陷入了同样的问题.感谢您的代码,我可以访问Gallery页面.幸运的是,我已经保存了图像.这是我的代码
  1. // when click Insert Gallery,run callback
  2. wp_media_frame.on('update',function(){
  3. var library = wp_media_frame.state().get('library');
  4. var images = [];
  5. var image_ids = [];
  6. thumb_wraper.html('');
  7.  
  8. library.map( function( image ) {
  9. image = image.toJSON();
  10. images.push(image.url);
  11. image_ids.push(image.id);
  12. thumb_wraper.append('<img src="' + image.url + '" alt="" />');
  13. });
  14. });

我发现你应该得到’图书馆’而不是’选择’.

编辑:
我已经想出如何回到画廊编辑.这是我的完整代码

  1. $( '#btn_upload' ).on( 'click',function( event ) {
  2. event.preventDefault();
  3.  
  4. var images = $( '#image_ids' ).val();
  5. var gallery_state = images ? 'gallery-edit' : 'gallery-library';
  6.  
  7. // create new media frame
  8. // You have to create new frame every time to control the Library state as well as selected images
  9. var wp_media_frame = wp.media.frames.wp_media_frame = wp.media( {
  10. title: 'My Gallery',// it has no effect but I really want to change the title
  11. frame: "post",toolbar: 'main-gallery',state: gallery_state,library: {
  12. type: 'image'
  13. },multiple: true
  14. } );
  15.  
  16. // when open media frame,add the selected image to Gallery
  17. wp_media_frame.on( 'open',function() {
  18. var images = $( '#image_ids' ).val();
  19. if ( !images )
  20. return;
  21.  
  22. var image_ids = images.split( ',' );
  23. var library = wp_media_frame.state().get( 'library' );
  24. image_ids.forEach( function( id ) {
  25. attachment = wp.media.attachment( id );
  26. attachment.fetch();
  27. library.add( attachment ? [ attachment ] : [] );
  28. } );
  29. } );
  30.  
  31. // when click Insert Gallery,run callback
  32. wp_media_frame.on( 'update',function() {
  33.  
  34. var thumb_wrapper = $( '#thumb-wrapper' );
  35. thumb_wraper.html( '' );
  36. var image_urls = [];
  37. var image_ids = [];
  38.  
  39. var library = wp_media_frame.state().get( 'library' );
  40.  
  41. library.map( function( image ) {
  42. image = image.toJSON();
  43. image_urls.push( image.url );
  44. image_ids.push( image.id );
  45. thumb_wrapper.append( '<img src="' + image.url + '" alt="" />' );
  46. } );
  47. } );
  48. } );

我想如果你重新打开现有的框架,它将始终保持初始状态,在你的情况下它是’画廊’.你必须每次都创建一个新的框架并检查是否有图像打开’gallery-edit’此外,我更喜欢’gallery-library’而不是’gallery’,因为我希望用户专注于我的画廊.

猜你在找的PHP相关文章