Site logo

Archived topic

I want to limit max file upload size in WP

8 replies · Started by Joanne Smith on April 9, 2016

Viewing posts 1–9 of 9

Theme Functions (functions.php)
Select theme to edit:

I added a new plugin to created a limit upload with some php code by the developer of another plugin on my site -- he said to add it to your theme function php -- you said to create your own plugin as the theme gets updates and then the PHP script I added gets deleted.

This is the CODE and below is the ERROR message

add_filter("adverts_gallery_upload_prefilter", "limit_file_uploads");

function limit_file_uploads( $file ) {
    if ( !isset($file["name"]) || !isset($file["type"]) ) {
        return $file;
    }
    if ( !isset( $_POST["post_id"] ) ) {
        $post_id = 0;
    } else {
        $post_id = intval($_POST["post_id"]);
    }

    if( $file["size"] >= 1000000 ) {
        $file["error"] = "Your file is to big.";
    }

    return $file;
}

I created a new plugin

and got this error message

Fatal error: Cannot redeclare limit_file_uploads() (previously declared in
/home/bobtail/www/www/wp-content/plugins/limit_file_uploads/limit_file_uploads.php:16)
in /home/bobtail/www/www/wp-content/plugins/max-file-upload/max-file-upload.php on line 29

Is there any other way you can think of that would limit the file size being uploaded on the site ?

Hi Joanne,

This is why it's always good to prefix functions to avoid conflict.

So you could do this instead:

add_filter("adverts_gallery_upload_prefilter", "joanne_limit_file_uploads");
function joanne_limit_file_uploads( $file ) {
    if ( !isset($file["name"]) || !isset($file["type"]) ) {
        return $file;
    }
    if ( !isset( $_POST["post_id"] ) ) {
        $post_id = 0;
    } else {
        $post_id = intval($_POST["post_id"]);
    }

    if( $file["size"] >= 1000000 ) {
        $file["error"] = "Your file is to big.";
    }

    return $file;
}

hm

I added this code to the site as a plugin and activated it

I tried a test UPLOAD of a 1.3MB image and it did not give me the error message

Can you test a dummy upload (( you don't have to submit )) just go to the upload button

https://www.bobtailclassifieds.com.au/adverts/post-an-ad/

just in case ??

Joanne

wait one sec - just noticed that it was NOT ACTIVATED in the plugins -- sorry about that

OK its WORKS

thanks

Joanne

One other thing ??

Why does it take 3 to 5 mins of SPINNING wheel before the error message comes up ??

Is it the script or something else ??

If you have the plugin turned off it only takes 30 secs to upload and image 2.5MB

hmmm strange

Joanne

I'm afraid I wouldn't know, it would be better to ask the develop as they have an understanding of what's going on behind the scenes.

How I can increase my wp media upload size? can I use php.in or other requirements here.

Hi there,

that is core WP and server level requirement - you can see some general advice on doing so here:

https://www.wpbeginner.com/wp-tutorials/how-to-increase-the-maximum-file-upload-size-in-wordpress/

But you should check with your host in case they have imposed any limits on that size. Or they may have a simple option to change it in their server control panel

If adding to a functions.php file then you MUST NOT edit the Theme files. You would need to install a child theme:

https://docs.generatepress.com/article/using-child-theme/

This archived topic is closed to new replies.