简体   繁体   English

使用自定义插入媒体插件的问题-Wordpress

[英]Issue using custom insert media for plugin - Wordpress

I have this sample code I am using to insert media as attachments for custom made orders. 我有此示例代码,用于插入媒体作为定制订单的附件。 I downloaded a Role Plugin which allowed me to be able to see Media but I don't want to see media of another user so I had a question about how to show only media I uploaded or this user uploaded. 我下载了一个角色插件,该插件使我能够查看媒体,但我不想看到其他用户的媒体,因此我对如何仅显示我上传的媒体或该用户上传的媒体有疑问。 But back to the main question. 但是回到主要问题。 This is the code I am using to open the media upload window once a button is clicked: 这是单击按钮后用于打开media upload窗口的代码:

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

  $('.upload_image_button').on('click', function( event ){

    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: true  // Set to true to allow multiple files to be selected
    });

    file_frame.on( 'select', function() {

        var selection = file_frame.state().get('selection');

        selection.map( function( attachment ) {

          attachment = attachment.toJSON();

          // Do something with attachment.id and/or attachment.url here
        });
     });

    // 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;
  });

Now the issue here is, once I upload an image. 现在的问题是,一旦我上传了图片, I just get an error returned which states An error occurred in the upload. Please try again later. 我刚收到一个错误,指出An error occurred in the upload. Please try again later. An error occurred in the upload. Please try again later. That is it. 这就对了。 I am not sure why I would be getting this error? 我不确定为什么会收到此错误?

Source code I got was from http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/ 我得到的源代码来自http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/

Suggestions, thoughts? 建议,想法?

you could try something in php....mess with this i cant remember the filenames offhand... 您可以在php中尝试某些操作。...这样的混乱,我记不起来副手使用的文件名...

function filter_media_files( $wp_query_obj ) {

     global $pagenow;
     global $current_user;

    if( !is_a( $current_user, 'WP_User') ) // users not admins
       return;

    if( 'upload.php' == $pagenow && 'media-upload.php' == $pagenow) {
       $wp_query_obj->set('author', $current_user->id );
    } else {
       return;
    }

return;
}

add_action('pre_get_posts', 'filter_media_files');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM