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

    //Is it an image?
      $override = array('test_form' => false);
      $file = wp_handle_upload($image,$override);         
      $plugin_options[$keys[$i]] = $file['url'];
   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?!?
 $options = get_option('theme_options');
 $plugin_options[$keys[$i]] = $options[$keys[$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.

  • Thx buddy!! Hats off...
    – Dejo Dekic
    Jun 1, 2013 at 23:40

This worked really nice for me:

   $error = new WP_Error( 'not_found', _('No image found! Please upload images only!'), 
        array( 'title' => _('Plugin Error'), 'response' => '406', 'back_link' => true ) );
        if( is_wp_error($error) ){
        wp_die( $error, '', $error->get_error_data() ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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