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

时间:2014-07-29 23:57:57

标签: javascript jquery wordpress

我有这个示例代码我用来插入媒体作为自定义订单的附件。我下载了一个角色插件,这使我能够看到媒体,但我不想看到其他用户的媒体,所以我有一个关于如何只显示我上传的媒体或这个用户上传的问题。但回到主要问题。这是我用来在点击按钮后打开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;
  });

现在我的问题是,上传图片后。我刚刚收到一个错误,表明An error occurred in the upload. Please try again later.就是这样。我不知道为什么我会收到这个错误?

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

建议,想法?

1 个答案:

答案 0 :(得分:1)

你可以在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');