1

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" )?

3

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.

| improve this answer | |
  • Thx buddy!! Hats off... – Dejo Dekic Jun 1 '13 at 23:40
2

This worked really nice for me:

 else{
   $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() ); 
        }
   }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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