Wp die causing 500 Internal Server Error?

I have function to upload images inside my settings API ,it uploads images as expected but the problem comes when user chooses to upload non-image file to stop users from uploading other files (non-image files) i am using wp_die('No image found'); an that leads to dreaded
500 Internal Server Error message displayed. Strange thing is that my wp_die wp_die('No image found'); is running fine on my local computer but not online.

function validate_setting($plugin_options){ 

  //echo '<pre>';  print_r($_FILES); echo '</pre>';
  $keys = array_keys($_FILES);
   $i = 0;
  foreach ($_FILES as $image) {        

if($image['size']){
    //Is it an image?
    if(preg_match('/(jpg|jpeg|png|gif)$/i',$image['type'])){
      $override = array('test_form' => false);
      $file = wp_handle_upload($image,$override);         
      $plugin_options[$keys[$i]] = $file['url'];
    }
   else{
   wp_die('No image found');//This triggers 500 Internal Server Error WHY??
       //echo:'no image found'; //Now this DON'T work locally but works ONLINE?!?
   }
}
else{
 $options = get_option('theme_options');
 $plugin_options[$keys[$i]] = $options[$keys[$i]];
}
$i++;
    } 
    return $plugin_options;
    }

I tried to change wp_die() to echo 'no image found'; and this happens:
now on my locall machine it echoes ‘nothing’ (no warning displayed), but now online it works and displays ‘no image found’ warning…

I know i could simply delete the else{
wp_die('No image found');
}
part and display no warning, but WHY it is not working as intended?

Can someone tell me what is wrong with my code and how to display warning with wp_die(‘No image found’) properly (without causing “500 Internal server error” )?

2 Answers
2

Use die($msg); to do what you’re trying to do. wp_die() returns an HTTP error code and passes your message to the error page, and the default code is 500 (internal server error) if you don’t set the return code in the args.

Leave a Comment