如何为WordPress中的每个用户设置自定义下载页面URL?

发布于 2025-02-13 20:38:11 字数 144 浏览 0 评论 0 原文

我希望我的WordPress网站像Mediafire一样工作。我已经有一个仅适用于登录用户的上传页面。上传的文件保存在:上传/用户名中。但是,我希望每个用户的下载页面唯一。我需要一种方法才能使页面仅显示其上传目录中的文件以显示给他(只有上载/用户名中的文件)。有什么办法吗?

I want my wordpress website to work like mediafire. I already have an upload page that works only for logged in users. The uploaded files are saved in: uploads/username. However I want the downloads page to be unique for every user. I need a way for the page to show only the files that are in his upload directory to be shown to him (only the files in uploads/username). Any way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2025-02-20 20:38:11

您可以在function.php文件中添加以下代码。

<?php 
add_filter( 'ajax_query_attachments_args', 
'wpb_show_current_user_attachments' );
function wpb_show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
') ) {
    $query['author'] = $user_id;
}
return $query;
} 
?>

请访问链接以获取更多详细信息。

You can add the following code in your function.php file.

<?php 
add_filter( 'ajax_query_attachments_args', 
'wpb_show_current_user_attachments' );
function wpb_show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
') ) {
    $query['author'] = $user_id;
}
return $query;
} 
?>

Visit this link for more details.

缘字诀 2025-02-20 20:38:11

我找到了一个WordPress问题(获取名称带有php 的目录中的所有文件)从我获得大部分代码的位置,然后添加了用户名函数。

<?php
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$path = "uploads/{$username}";
$files = scandir($path);
foreach ($files as &$value) {
    echo "<a href='http://example.com/{$path}/".$value."' target='_blank' >".$value."</a><br/><br/>";
}
?>

I found a wordpress question (Getting the names of all files in a directory with PHP) from where I got most of the code and then i added the user name function.

<?php
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$path = "uploads/{$username}";
$files = scandir($path);
foreach ($files as &$value) {
    echo "<a href='http://example.com/{$path}/".$value."' target='_blank' >".$value."</a><br/><br/>";
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文