WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When users upload very large photos and memory is tight, it seems like Wordpress runs out of memory - fails to resize the uploaded photos and does not create the necessary metadata (_wp_attachment_metadata entry in wp_postmeta is not created).

The worst part is that the user is never notified. At most I get an "HTTP error" message.

Is is possible to somehow add an error message that will warn the user and remove the inconsistent file/database entries? How come this is not standard WP behavior?

share|improve this question
I'd probably start by turning on WP_DEBUG (and following the rest of the Debugging instructions) and see what WordPress reports when it runs into the memory issues. – Pat J Mar 23 '13 at 21:21
It would be better to fix this instead of showing an error. Can't you increase PHP's memory limit? That did the trick for a few issues like those for me. You could try adding ini_set('memory_limit', '128M'); to your config file. Note that not all hosts will allow you to increase the memory limit this way. If that's the case, you could ask the host if they will increase the memory limit. – Camil Staps Mar 26 '13 at 12:24
up vote 0 down vote accepted

How do your users upload photos? You're right about register_shutdown_function. It seems to work well in my code, though I only ever call my back-end via AJAX (I've also got file upload functionality), and so only need to account for this case. Here's how I set it up in my plugin. Outside of the plugin class, somewhere at the bottom of the file, I have a function:

function fatal_handler_ajax_wrapper() {
    $error = error_get_last();
    if( $error !== NULL) {
        $ret = array('status' => -1, 'msg' => 'Fatal error on server. Please contact server admin.');
        $errfile = "unknown file";
        $errstr  = "shutdown";
        $errno   = E_CORE_ERROR;
        $errline = 0;

        $ret['details'] = $error;
    echo json_encode($ret);

In my plugin constructor I hook it up like this:

function __construct() {
    register_shutdown_function( "fatal_handler_ajax_wrapper" );
    // ... whatever else you may need to do in your constructor

I've successfully tested it using this SO question's code (produces a nice fatal error). Put it somewhere inside your AJAX handler function in the back-end and watch the back-end return a nice JSON object to the front-end:

$a = 'x';
while (true) {
    $a = $a.$a;

Edit: just discovered from this SO question that register_shutdown_function will execute pretty much every time, even if there is no error. I've edited the code snippet to reflect this fact.

share|improve this answer
I'll give you the bounty since you did the work to validate the register_shutdown_function idea. I'm still not sure why this isn't part of the core Wordpress code. Users are uploading photos using WP media manager which is part of core WP and should handle OOM conditions better. – nimrodm Mar 28 '13 at 20:05
@nimrodm Thanks! Just a guess, but perhaps it simply has not been a priority for the core dev team. – montrealist Mar 28 '13 at 20:09

I'm gonna add this as an answer, though it's probably not the answer you want.

The answer is "No, it is not possible".

Out of memory errors in PHP are fatal errors. There is no recovering from a fatality, and so no way to return a useful error message.

share|improve this answer
Is it? I believe register_shutdown_function can be used to handle fatal errors too. – nimrodm Mar 27 '13 at 21:48

@vancoders answer is right in general, but if you can impose restrictions on the size of the image then you can validate that the system will not fail, immediately after the image upload.

The important thing here is not to check the file size itself but rather the dimension of the image, as the amount of memory required to prpcess it is bigger when the image in bigger.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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