据我所知,我已经为您编写了一个简单的系统。请在您的functions.php
-
add_action( \'wp_enqueue_scripts\', \'the_dramatist_enqueue_scripts\' );
add_filter( \'ajax_query_attachments_args\', \'the_dramatist_filter_media\' );
add_shortcode( \'the_dramatist_front_upload\', \'the_dramatist_front_upload\' );
/**
* Call wp_enqueue_media() to load up all the scripts we need for media uploader
*/
function the_dramatist_enqueue_scripts() {
wp_enqueue_media();
wp_enqueue_script(
\'some-script\',
get_template_directory_uri() . \'/js/media-uploader.js\',
// if you are building a plugin
// plugins_url( \'/\', __FILE__ ) . \'/js/media-uploader.js\',
array( \'jquery\' ),
null
);
}
/**
* This filter insures users only see their own media
*/
function the_dramatist_filter_media( $query ) {
// admins get to see everything
if ( ! current_user_can( \'manage_options\' ) )
$query[\'author\'] = get_current_user_id();
return $query;
}
function the_dramatist_front_upload( $args ) {
// check if user can upload files
if ( current_user_can( \'upload_files\' ) ) {
$str = __( \'Select File\', \'text-domain\' );
return \'<input id="frontend-button" type="button" value="\' . $str . \'" class="button" style="position: relative; z-index: 1;"><img id="frontend-image" />\';
}
return __( \'Please Login To Upload\', \'text-domain\' );
}
在主题文件夹中创建一个名为
js
并在文件夹中创建一个名为
media-uploader.js
. 内部
media-uploader.js
文件放置以下代码-
(function($) {
// When the DOM is ready.
$(function() {
var file_frame; // variable for the wp.media file_frame
// attach a click event (or whatever you want) to some element on your page
$( \'#frontend-button\' ).on( \'click\', function( event ) {
event.preventDefault();
// if the file_frame has already been created, just reuse it
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.file_frame = wp.media({
title: $( this ).data( \'uploader_title\' ),
button: {
text: $( this ).data( \'uploader_button_text\' ),
},
multiple: false // set this to true for multiple file selection
});
file_frame.on( \'select\', function() {
attachment = file_frame.state().get(\'selection\').first().toJSON();
// do something with the file here
$( \'#frontend-button\' ).hide();
$( \'#frontend-image\' ).attr(\'src\', attachment.url);
});
file_frame.open();
});
});
})(jQuery);
实际上,上面的全部内容最终给了您一个名为
the_dramatist_front_upload
. 将此短代码放在您想要查看媒体管理器的位置。并且它将仅向登录的用户显示媒体管理器。希望这件事对你有帮助。我从
here