Home > front end >  How can set a custom download page url for each user in wordpress?
How can set a custom download page url for each user in wordpress?

Time:07-08

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?

CodePudding user response:

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.

CodePudding user response:

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/>";
}
?>
  • Related