I restrict image uploads by file size and dimensions on my client's websites using 'wp_handle_upload_prefilter'.

However, it appears this function is not called when using the WordPress.com mobile app. Authors who use this mobile app method for posting are allowed to upload any size and dimensions via the media library and "featured image" sections.

I assume the mobile app uses the REST API, rather than conventional frontend code. I haven't been able to find any filters for uploads using the REST API. Does anyone know of a workaround?

I understand there are methods of reducing overall file upload sizes across WordPress. This is not a question for the 'upload_size_limit' filter. I restrict both file size and image height/width for images. I also implement a different file size restriction for audio files, etc. I've exhausted research in finding a way to stop my authors from circumventing these restrictions using the WP app, which I assume uses REST. I do not want to prohibit them from using it though.

Oh, I should mention. We are NOT hosted on wp.com. We are using the app to login to our hosted website.

  • when you use the wp.com mobile app, the app is talking to the wp.com API, not your site, and it's servers at WordPress.com that then do the API request. This is done so that the app is fast and isn't unusable when people have lots of plugins that slow down the Jetpack REST endpoints or XMLRPC. Unfortunately this also puts it outside this stacks scope as to fix this you'll need help with knowledge of Automattics 3rd party services and systems. Also the REST API is used by the block editor, so you can test the theory without using mobile apps
    – Tom J Nowell
    Commented Feb 12, 2023 at 14:21
  • Instead, would it not be easier to use upload_size_limit only for size limits, and then use the image sizes API to remove sizes you don't want? There's no code to work with or debug in your question so it's difficult to confirm things, and it's unclear why you have this restriction which makes suggesting alternatives difficult ( is it for editorial reasons? are you on a host with limited disk space and trying to avoid it filling up? is this for pagespeed/frontend performance reasons? )
    – Tom J Nowell
    Commented Feb 12, 2023 at 14:27
  • Good question @TomJNowell. And thanks for the answer. We want to restrict uploaded images from the app because they are typically taken on the device and unedited. These images can be ginormous (3000px x 4000px), which makes for huge file sizes. In regards to dimensions, we're a professional news site. When images are uploaded in any dimensions, they do not appear well - anywhere. Take a vertical image from a phone as an example. Surely, there is some action with WP that happens before the image is saved?
    – Jim S.
    Commented Feb 12, 2023 at 17:19
  • can you include the missing code? It's missing but it's also the most important part of your question. There's also still an extremely high chance that this question is off-topic and should be closed, code will help determine if that is the case, as well as confirm if you've made a particular set of mistakes
    – Tom J Nowell
    Commented Feb 13, 2023 at 10:59

1 Answer 1


Try this code in function.php I hope this help.

add_action( 'rest_api_init', 'set_image_dimension_limit' );
function set_image_dimension_limit() {
    add_filter( 'wp_handle_upload_prefilter', function( $file ) {
         $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );

        if( !in_array( $file['type'], $mimes ) )
        return $file;

        $img = getimagesize( $file['tmp_name'] );
        $maximum = array( 'width' => 500, 'height' => 700 );

        if ( $img[0] > $maximum['width'] )
        $file['error'] = 
            'Image too large. Maximum width is ' 
            . $maximum['width'] 
            . 'px. Uploaded image width is ' 
            . $img[0] . 'px';

        elseif ( $img[1] > $maximum['height'] )
        $file['error'] = 
            'Image too large. Maximum height is ' 
            . $maximum['height'] 
            . 'px. Uploaded image height is ' 
            . $img[1] . 'px';

        return $file;
    } ); 
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 21, 2023 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.