0

I'm working on a WordPress single site, where in front end, below a custom made form, where I have a file upload feature, I need to display the maximum upload size.

In my local Laragon WAMP setup, the PHP post_max_size is set to 2G. I wanted to downgrade to only 20M (20 mb).

After searching and reading several blog articles, I put the following code in my theme's functions.php:

<?php
// Set upload size limit to 20mb.
@ini_set('upload_max_size', '20M');
@ini_set('post_max_size', '20M');
@ini_set('max_execution_time', '300');

/**
 * Filter the upload size limit for non-administrators.
 *
 * @param string $size Upload size limit (in bytes).
 *
 * @return int         Filtered size limit.
 */
function wpse_control_upload_size_limit( $size ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return 20971520; // 20mb to binary bytes.
    }

    return $size;
}

add_filter( 'upload_size_limit', 'wpse_control_upload_size_limit', 20 );

With this code in action, with a non-admin user I can see the Admin panel Upload New Media page is showing: Maximum upload file size: 20 MB.

Issue is: I want to display the same maximum file size limit information on my front end from.

What I tried so far is putting the following code in functions.php:

var_dump(@ini_get('post_max_size'));

But this is getting the 2G limit as it's not getting the WordPress setup.

I also tried by hooking this code in upload_size_limit with no luck:

add_action( 'upload_size_limit', function() {
    var_dump( @ini_get( 'post_max_size' ) );
});

I know there's a plugin named Increase Maximum Upload File Size, but I don't want to use a plugin for this feature. I'm also aware that, in multisite we've setup data from where we can get the maximum upload size.

But how can I display the maximum upload size on a front page form in single WordPress instance?

1 Answer 1

0

The answer actually lies on the same page where you inspected your modifications: /wp-admin/media-new.php.

If you get to the /wp-admin/media-new.php file, you will get a function:

<?php media_upload_form(); ?>

And the function is located in /wp-admin/includes/media.php, where you will get the following line:

$max_upload_size = wp_max_upload_size();

So the holly grail you are searching is: wp_max_upload_size().

So you can easily follow the core, and display the maximum uploaded size anywhere with the following bit of code (thanks to the core):

<?php
$max_upload_size = wp_max_upload_size();
if ( ! $max_upload_size ) {
    $max_upload_size = 0;
}

/* translators: %s: Maximum allowed file size. */
printf( __( 'Maximum upload file size: %s.', 'wpse355118' ), esc_html( size_format( $max_upload_size ) ) );
?>

Your Answer

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

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