首先是观察,使用$upload_dir.\'/USER_PHOTOS\'.$user->user_login
可能会导致文件夹混乱wp-content/
.
我建议wp-content/user-photos/user-login/
. 下面的例子就是这样考虑的。
在您的情况下,您已经有了菜单项,为了完整起见,这是用于开发的子菜单:
add_action( \'admin_menu\', \'wpse_75873_folder_menu\' );
function wpse_75873_folder_menu()
{
add_media_page(
\'User Photos Media\',
\'User Photos\',
\'edit_posts\',
\'so-13416177\',
\'wpse_75873_display_page\'
);
}
这是为用户显示的包含文件夹内容的页面,以及一些辅助功能:
function wpse_75873_display_page()
{
echo \'<div id="icon-upload" class="icon32"></div><h2>User Photos</h2>\';
$baseDir = WP_CONTENT_DIR . \'/user-photos/\';
$baseUrl = WP_CONTENT_URL . \'/user-photos/\';
// build user folder
global $current_user;
get_currentuserinfo();
$userDir = $baseDir . $current_user->data->user_login . \'/\';
$userUrl = $baseUrl . $current_user->data->user_login . \'/\';
if( !is_dir( $userDir ) )
{
echo \'<div style="margin-top:30px;font-size:2em;font-weight:bold;color:#f00">
Folder does not exist!
</div>\';
return;
}
// build array with files in user folder
$files = array();
if ( $dir = opendir( $userDir ) )
{
while ( $file = readdir( $dir ) )
{
if ( $file != "." && $file != \'..\' )
{
if ( !is_dir( $userDir . $file ) )
{
// Hide files that start with a dot
if( !so_834303_starts_with( $file, \'.\' ) )
{
$size = so_13416177_file_size(
filesize( $userDir . $file )
);
$files[] = array( $file, $size );
}
}
}
}
closedir($dir);
}
if ( empty( $files ) )
{
echo \'<div style="margin-top:30px;font-size:2em;font-weight:bold;color:#00f">
No photos!
</div>\';
}
else
{
?>
<table class="widefat">
<thead>
<tr>
<th style="width:20%">Thumbnail</th>
<th>File</th>
<th>Size</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Thumbnail</th>
<th>File</th>
<th>Size</th>
</tr>
</tfoot>
<tbody>
<?php
foreach ($files as $file)
{
?>
<tr>
<td><!-- THIS IS NOT A THUMBNAIL -->
<?php echo \'<img src="\' . $userUrl . $file[0] . \'" width="50">\'; ?>
</td>
<td>
<a href="<?php echo $userUrl . $file[0]; ?>">
<?php echo $file[0]; ?>
</a>
</td>
<td><b><?php echo $file[1]; ?></b></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} // end else
}
// https://stackoverflow.com/q/834303
function so_834303_starts_with( $haystack, $needle )
{
return !strncmp( $haystack, $needle, strlen( $needle ) );
}
// http://www.php.net/manual/en/function.filesize.php#110739
function so_13416177_file_size( $o, $depth=0 )
{
if( $o > 1024 )
return so_13416177_file_size( $o/1024, $depth+1 );
$unit = array( \'B\', \'KB\', \'MB\', \'GB\', \'TB\', \'PB\', \'EB\', \'PB\', \'ZB\', \'YB\' );
return sprintf( \'%.01f %s\', $o, $unit[$depth] );
}
The result:
基于recent Answer in StackOverflow. 对代码进行了修改和改进