How can PHP scripts in Wordpress effectively identify and interact with logged-in users for personalized actions like creating user-specific upload directories?

To effectively identify and interact with logged-in users in WordPress for personalized actions like creating user-specific upload directories, you can use the WordPress functions to check if a user is logged in and get their user ID. Once you have the user ID, you can use it to create a unique directory for each user to upload files to.

<?php
// Check if user is logged in
if ( is_user_logged_in() ) {
    // Get the current user's ID
    $user_id = get_current_user_id();
    
    // Create a unique directory for the user
    $upload_dir = wp_upload_dir();
    $user_upload_dir = $upload_dir['basedir'] . '/user_' . $user_id;
    
    // Check if the directory exists, if not create it
    if ( ! file_exists( $user_upload_dir ) ) {
        mkdir( $user_upload_dir );
    }
}
?>