Copy URL

Share this on your favorite social networks:

Please log in to use this feature.
Email To Yourself Or A Developer
GenID: 3724

Limiting Media Uploads for Specific User ID with Custom Error Message

function cwpai_limit_media_upload_size($file) {
    if (get_current_user_id() == 5) {
        $size = $file['size'];
        $size = $size / 1024;
        if ($size > 1024) {
            $file['error'] = "The current plan doesn't allow for uploads over 1mb";
    return $file;
add_filter('wp_handle_upload_prefilter', 'cwpai_limit_media_upload_size');

Snippet Explanation

This code is written in PHP. The code does the following:
1. Checks if the current user is user ID 5
2. Gets the size of the file being uploaded
3. Converts the file size to kilobytes
4. If the size is over 1 megabyte (1024 kilobytes), it sets an error message
5. Returns the filtered file

The purpose of this code is to limit the size of files that user 5 can upload to the WordPress media library.

To install this code on WordPress, you have two options:
1. Add it to your functions.php file in the theme directory
2. Install a snippet management plugin and add the code as a snippet.

To use the code on WordPress, you will need to be logged in as the user with ID 5. Once the code is installed, this user will be restricted to uploading files that are no bigger than 1mb.

Read More
publishLoad Prompt
Make Public
Share Snippet
Export Snippet
Mode: WordPress PHP
Language: PHP
Visibility: Public
December 6, 2022
@James LePa...