也许没有必要将文件添加到库中。我们只需扫描给定文件夹并打印<video>
标记我们找到的所有视频文件。
如果需要密码,我会将订户帐户提供给用户,并在后端显示内容。在每个用户配置文件中,管理员可以select the folders that they\'ll have access to.
下面是一个概念证明,假设:
我们手动上传到内部的子文件夹wp-content/videos/
.
用户有权访问wp-content/videos/chapter1/
和wp-content/videos/chapter2/
.
一些solution is found 阻止外部访问此文件夹/文件。
<?php
/* Plugin Name: Video Page for Subscribers */
add_action( \'admin_menu\', \'admin_menu_wpse_114998\' );
# Custom admin page
function admin_menu_wpse_114998()
{
add_dashboard_page(
\'Video Library\',
\'Video Library\',
\'read\', // visible to Subscribers
\'video-library\',
\'menu_callback_wpse_114998\'
);
}
# Print the admin page
function menu_callback_wpse_114998()
{
$paths = user_folders_wpse_114998( get_current_user_id() );
foreach( $paths as $path )
print_video_folder_wpse_114998( $path );
}
# Get the user\'s allowed folders
function user_folders_wpse_114998( $user_id )
{
// $user_folders = do_your_thing_to_get_it();
$user_folders = array( \'chapter1\', \'chapter2\' );
return $user_folders;
}
# Iterate through a directory and print all MP4 videos
function print_video_folder_wpse_114998( $folder )
{
$path = WP_CONTENT_DIR . \'/videos/\' . $folder;
$files = array();
$directory = new RecursiveDirectoryIterator( $path );
$objects = new RecursiveIteratorIterator( $directory, RecursiveIteratorIterator::SELF_FIRST );
foreach( $objects as $name => $fileinfo )
{
if ( !$fileinfo->isFile() )
continue;
if( false === strpos( $name,\'.mp4\' ) )
continue;
$files[] = array(
\'url\' => WP_CONTENT_URL . "/videos/$folder/" . $fileinfo->getFilename(),
\'name\' => $fileinfo->getFilename()
);
}
foreach( $files as $file )
{
?> <div style="margin:25px 25%;background-color:#cecece;text-align:center">
<h2><?php echo $file[\'name\']; ?></h2>
<video src="<?php echo $file[\'url\']; ?>" controls>
Your browser does not support the <code>video</code> element.
</video>
</div>
<?php
}
}