I am creating a photo plugin for users to view their photos. The plugin works in a way that when I add a new user, a directory is created in the uploads folder inside wp-content
with the user’s username as the name of the folder. This is where that user’s images will be stored.
Here is the code:
function create_photo_folder() {
$upload = wp_upload_dir();
$upload_dir = $upload['basedir'];
$upload_dir = $upload_dir . '/USER_PHOTOS';
if (! is_dir($upload_dir)) {
mkdir( $upload_dir, 0700 );
}
$blogusers = get_users('blog_id=1&orderby=nicename');
foreach ($blogusers as $user) {
wp_mkdir_p($upload_dir . '/USER_PHOTOS'.$user->user_login);
}
}
register_activation_hook( __FILE__, 'create_photo_folder' );
add_action('user_register','create_photo_folder');
The above code works fine in creating the folders. The problem I am facing is that I want to be able to fetch the contents in the folder of each user and display it to them when they log in.
I have already created a menu page called YOUR PHOTOS for all users. Now I would like to be able to retrieve the content in each folder for each user and display it to them there.
First an observation, using
$upload_dir.'/USER_PHOTOS'.$user->user_login
may lead to a mess of folders inwp-content/
.I suggest
wp-content/user-photos/user-login/
. And the following example considers it this way.In your case, you already have the menu item, for completeness this is the submenu used for development:
And this the page displayed for the user with the contents of his folder, and a couple of auxiliary functions:
The result:
Based on a recent Answer in StackOverflow. The code is adapted and improved.