0

I have the following code to increase the upload size only when editing certain, special pages:

function my_edit_page_form( $post ) {
    if( my_is_special_page( $post ) ) {
        add_filter( 'upload_size_limit', 'my_upload_size_limit' );
    }
}
add_action( 'edit_page_form', 'my_edit_page_form' );

function my_upload_size_limit( $size ) {
    return 1024 * 18000;
}

On one of those pages, I have a Gallery shortcode. When I click on the Edit button, the "Maximum upload" message looks correct, but the upload is denied:

Maximum upload file size: 18 MB -- but 13 MB test.jpg exceeds the maximum upload size for this site.

When viewing the source, I see that max_file_size set is to 8388608b, the default for the site. This value comes from wp_plupload_default_settings. This means this function must be called before the edit_page_form hook.

So, if I cannot use edit_page_form, what can I use instead that will be called before wp_plupload_default_settings in order to check the post? Or, alternatively, should I be checking the post from within upload_size_limit? If yes to the latter, then how do I do that?

-- UPDATE --

Here is my final code:

function my_upload_size_limit( $size ) {
    global $post;
    if ( $post && $post->filter == 'edit' && $post->post_type == 'page' &&
        my_is_special_page( $post ) ) {
        $size = 1024 * 18000;
    }
    return $size;
}
add_filter( 'upload_size_limit', 'my_upload_size_limit' );
1

I think you are on the right track with your idea of checking if the right page is being viewed within the callback attached to the upload_size_limit hook.

This code demonstrates changing the upload size if one of the special pages is being viewed, and returning the standard max upload size otherwise:

function wpse239631_change_upload_size( $u_bytes, $p_bytes ) {
    // Array of post IDs where the upload size will be changed.
    $special_pages = array(
        1829, // change to your page
        1800, // change to your page, etc
    );

    // If we're on a special page, change the upload size,
    // otherwise, use the default max upload size.
    if ( in_array( get_the_ID(), $special_pages ) ) {
        return 1024 * 18000;
    } else {
        return min( $u_bytes, $p_bytes );
    }
}
add_filter( 'upload_size_limit', 'wpse239631_change_upload_size', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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