How do I increase the upload size only when editing special pages?

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:

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' );

Topic plupload post-editor uploads php gallery Wordpress

Category Web


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 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.