我希望显示和限制每个作者的磁盘使用情况,并能够作为管理员管理磁盘使用情况。
示例:author1:磁盘空间为20Mb;如果上传的数据超过20Mb,则会禁用上传按钮(在媒体库和post中)。
author2:磁盘空间为30Mb,如果上载的磁盘空间超过30Mb,则会禁用上载按钮(在媒体库和post中)。
我希望显示和限制每个作者的磁盘使用情况,并能够作为管理员管理磁盘使用情况。
示例:author1:磁盘空间为20Mb;如果上传的数据超过20Mb,则会禁用上传按钮(在媒体库和post中)。
author2:磁盘空间为30Mb,如果上载的磁盘空间超过30Mb,则会禁用上载按钮(在媒体库和post中)。
好的,这只是一个如何存档的示例。。
首先将上载目录更改为当前用户用户名。例如上传/管理
function wpse_16722_type_upload_dir( $args ) {
// Get current user data
$current_user = wp_get_current_user();
// Make upload dir to current username
$newdir = \'/\' . $current_user->user_login;
$args[\'path\'] = str_replace( $args[\'subdir\'], \'\', $args[\'path\'] ); //remove default subdir
$args[\'url\'] = str_replace( $args[\'subdir\'], \'\', $args[\'url\'] );
$args[\'subdir\'] = $newdir;
$args[\'path\'] .= $newdir;
$args[\'url\'] .= $newdir;
return $args;
}
add_filter( \'upload_dir\', \'wpse_16722_type_upload_dir\' );
然后我们需要一个函数来检查当前用户上传的大小。我发现一个函数recurse_dirsize
在内部ms-functions.php 以字节为单位计算目录。/**
* Get the size of a directory recursively.
*
* Used by get_dirsize() to get a directory\'s size when it contains
* other directories.
*
* @since MU
*
* @param string $directory
* @return int
*/
function recurse_dirsize( $directory ) {
$size = 0;
$directory = untrailingslashit( $directory );
if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
return false;
if ($handle = opendir($directory)) {
while(($file = readdir($handle)) !== false) {
$path = $directory.\'/\'.$file;
if ($file != \'.\' && $file != \'..\') {
if (is_file($path)) {
$size += filesize($path);
} elseif (is_dir($path)) {
$handlesize = recurse_dirsize($path);
if ($handlesize > 0)
$size += $handlesize;
}
}
}
closedir($handle);
}
return $size;
}
然后,我们需要检查当前用户上传的内容是否大于x,如果是,请禁用media_buttons
function wpse_16722_user_upload_size() {
// Get current user data
$current_user = wp_get_current_user();
// Array of key => value pairs
$uploads = wp_upload_dir();
$dir = $uploads[\'basedir\'] . \'/\' . $current_user->user_login;
if( file_exists( $dir ) && is_dir( $dir ) ) {
$size = recurse_dirsize( $dir ) / 1024 / 1024;
// Convert bytes to human readable format
$new_size = round( $size / 1024 * 1024, 2 );
// If current users upload dir is
// Bigger than 20 MB disable
// Upload buttton
if( $new_size >= 20 ) {
remove_action( \'media_buttons\', \'media_buttons\' );
}
}
}
add_action(\'admin_head\', \'wpse_16722_user_upload_size\');
您还希望控制多个用户和不同的大小。我建议您添加一个设置页面,用以保存用户可用大小update_user_meta($user_id, \'size_limit\', $value );
了解更多信息here当您将尺寸添加到users_meta
您可以在函数中更改大小wpse_16722_user_upload_size
“20”至以下尺寸get_user_meta( $user_id, \'size_limit\');
// this function disable media buttons
function removemediabuttons()
{
remove_action( \'media_buttons\', \'media_buttons\' );
}
// this function check the total size of all attachments by user.
//But it doesnt count sizes of thumbnails.
// $limitBytes -- maximum size in bytes
function checkMidiaSizeByUser($userId, $limitBytes)
{
// get all attachments of $userId
$attachements = query_posts(
array(
\'post_type\' => \'attachment\',
\'author\' => $userId
)
);
// count size
$size = 0;
foreach($attachements as $attachment){
$size += filesize( get_attached_file( $attachment->ID ) );
}
// check limit
if ( $size >= $limitBytes ) {
add_action(\'admin_head\',\'removemediabuttons\');
}
}
/* **************************************** */
// Set our limit to 20Mb
$limit = 20*1024*1024;
// get current user
global $current_user;
get_currentuserinfo();
//check limit and disable media buttons if user met limit
checkMidiaSizeByUser($current_user->ID, $limit);
我不会在实际工作中测试此代码,但它应该可以工作。我有一个插件,可以调用媒体上传程序,允许用户向页面添加图像。上传程序正在显示并拍摄图像,但它没有如我所愿将其附加到页面ID 65(或在图库中显示附加到该页面的图像)。我是否遗漏了什么,或者这在WP中是不可能的?谢谢jQuery(document).ready(function($){ $(\'.manage-images-button a.button-secondary\').on(\'click\', function(){ tb_show(