Reputation: 2801
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:
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.
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/
Suggestions, thoughts?
Upvotes: 0
Views: 412
Reputation: 5937
you could try something in php....mess with this i cant remember the filenames offhand...
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');
Upvotes: 1