Home / Attachments / Limit user files in media library
Duplicate Snippet

Embed Snippet on Your Site

Limit user files in media library

change the number in the snippet

<10
Code Preview
php
<?php
function limit_media_files($file) {
    // Get the current user ID
    $current_user_id = get_current_user_id();
    
    // Query media files for the current user
    $media_files = new WP_Query(array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
        'author' => $current_user_id
    ));
    
    // Check if the user has already uploaded 5 or more files
    if ($media_files->found_posts >= 5) {
        $file['error'] = __('You have reached the limit of 5 files in your media library', 'textdomain');
    }
    return $file;
}
add_filter('wp_handle_upload_prefilter', 'limit_media_files');

Comments

Add a Comment