How to restrict wp-admin and prevent upload errors

I need to restrict wp-admin access to all users who cant manage_options and still let users add media on the front end.

function wpse_11244_restrict_admin() {
    if ( ! current_user_can( 'manage_options' )  && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
        wp_redirect( home_url() );
    }
}
add_action( 'admin_init', 'wpse_11244_restrict_admin', 1 );

I found this code on stack overflow and it works great for my disabling the backend. The only hiccup I have found is when I go to add media on a front end post I get the following error.

media upload error

When I refresh the page and click add media I now see the image that showed the upload error.

enter image description here

The image does upload but the only way the user would know that is if they refreshed.

How can I disable backend access for everything and still allow them to upload media?

Solutions Collecting From Web of "How to restrict wp-admin and prevent upload errors"

You just need one extra thing for this.

Here is the code I typically use to do what you are doing:

function pws_block_admin() {

    if (
        // Look for the presence of /wp-admin/ in the url
        stripos($_SERVER['REQUEST_URI'],'/wp-admin/') !== false
        &&
        // Allow calls to async-upload.php
        stripos($_SERVER['REQUEST_URI'],'async-upload.php') === false
        &&
        // Allow calls to admin-ajax.php
        stripos($_SERVER['REQUEST_URI'],'admin-ajax.php') === false
    ) {
        if ( !current_user_can('manage_options') ) {
            $redirect_to = home_url();
            wp_redirect($redirect_to, 302);
        }
    }
}
add_action('admin_init', 'pws_block_admin', 0);

It looks like you are just missing the exclusion for allowing async-upload.php to work.