如何将v3.5媒体库模式中的图像限制为仅来自特定帖子ID的图像?

时间:2013-02-26 作者:m-torin

如何限制v3中出现的图像。5媒体库模式,仅显示附加到特定帖子id的内容?

我正在创建一个前端管理模板,允许多个作者编辑任何特定的帖子,因此需要限制每篇帖子上显示的内容,而不是特定用户上传的内容。

上载模式基于Mike Jolley\'s upload modal tutorial. 它被修改为在body类中查找post id,并将上传的媒体附加到$pid。

这是迄今为止完整的modal js:

// Uploading files
jQuery(document).ready(function($) {

  var file_frame;
  var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id

  var classes = $(\'body\').attr(\'class\'); // get all classes from <body> element
  var set_to_post_id = classes.match(/postid-(\\d+)/)[1]; // pid to attach media to

  jQuery(document).on(\'click\', \'.upload_image_button\', function(){

    event.preventDefault();

    // If the media frame already exists, reopen it.
    if ( file_frame ) {
      // Set the post ID to what we want
      file_frame.uploader.uploader.param( \'post_id\', set_to_post_id );
      // Open frame
      file_frame.open();
      return;
    } else {
      // Set the wp.media post id so the uploader grabs the ID we want when initialised
      wp.media.model.settings.post.id = set_to_post_id;
    }

    // Create the media frame.
    file_frame = wp.media.frames.file_frame = wp.media({
      title: jQuery( this ).data( \'uploader_title\' ),
      button: {
        text: jQuery( this ).data( \'uploader_button_text\' ),
      },
      multiple: false  // Set to true to allow multiple files to be selected
    });

    // When an image is selected, run a callback.
    file_frame.on( \'select\', function() {
      // We set multiple to false so only get one image from the uploader
      attachment = file_frame.state().get(\'selection\').first().toJSON();

      // Do something with attachment.id and/or attachment.url here

      // Restore the main post ID
      wp.media.model.settings.post.id = wp_media_post_id;
    });

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

  });

  // Restore the main ID when the add media button is pressed
  jQuery(\'a.add_media\').on(\'click\', function() {
    wp.media.model.settings.post.id = wp_media_post_id;
  });

});
媒体库上的两个主要权威WPSE线程只处理用户的限制。

2 个回复
SO网友:Ünsal Korkmaz

我不确定这就是你要找的。此代码将;“锁定”;上载以在媒体面板中仅显示“上载到此帖子”

add_action( \'admin_footer-post-new.php\', \'firmasite_mediapanel_lock_uploaded\' );
add_action( \'admin_footer-post.php\', \'firmasite_mediapanel_lock_uploaded\' );
function firmasite_mediapanel_lock_uploaded() { ?>
  <script type="text/javascript">
    jQuery(document).on("DOMNodeInserted", function(){
        // Lock uploads to "Uploaded to this post"
        jQuery(\'select.attachment-filters [value="uploaded"]\').attr( \'selected\', true ).parent().trigger(\'change\');
    });
  </script>
<?php }

SO网友:cybmeta

我花了两天的时间寻找解决方案,来自Ünsal Korkmaz的答案对我来说并不适用。最后,我找到了正确的答案,我想与大家分享。

只是想澄清一下,Ünsal Korkmaz的回答将在媒体管理器窗口中预先选择“上传到此帖子”选项,但如果您在自己的插件、主题、自定义元框等中使用媒体管理器,并且您正在构建自己的媒体管理器框架,这将不起作用。即使它可以工作,您也会有一个预选的过滤器,但这并不能有效地将媒体库限制为定义的post附件。

以下是我找到的解决方案:

  //Chame the selector to fit your code
  jQuery(\'#manage-gallery-button\').click(function(e) {

         e.preventDefault();
         var frame = wp.media({
                       title : \'Pick the images for the gallery of this entry\',
                       frame: \'select\',
                       multiple : true,
                       library : {
                                    type : \'image\',
                                    //HERE IS THE MAGIC. Set your own post ID var
                                    uploadedTo : wp.media.view.settings.post.id
                                  },
                       button : { text : \'Insert\' }
                   });

                   frame.on(\'close\',function() {
                      // get selections and save to hidden input plus other AJAX stuff etc.
                      var selection = frame.state().get(\'selection\');
                      var gallery_ids = new Array();
                      var my_index = 0;
                      selection.each(function(attachment) {
                         gallery_ids[my_index] = attachment[\'id\'];
                         my_index++;
                      });
                      var ids = gallery_ids.join(",");
                      //Store ids in my hidden input
                      jQuery(\'#gallery-ids\').val(ids);
                      Refresh_Gallery(ids);
                   });

                  frame.on(\'open\',function() {
                    //Preselect attachements from my hidden input
                    var selection = frame.state().get(\'selection\');
                    ids = jQuery(\'#gallery-ids\').val().split(\',\');
                    ids.forEach(function(id) {
                      attachment = wp.media.attachment(id);
                      attachment.fetch();
                      selection.add( attachment ? [ attachment ] : [] );
                    });

                  });

                frame.open();
 });

结束