0

I get a 500 error after I've added the wp_handle_upload_prefilter to my plugin class. How I can fix this?

require_once 'vendor/autoload.php';

class MyClass {

  public function __construct()
  {
    add_action( 'wp_enqueue_scripts', array($this, 'main') );
    add_action( 'wp_ajax_new_message', array($this, 'process_upload') );
    add_action( 'wp_ajax_nopriv_new_message', array($this, 'process_upload') );
    add_filter( 'wp_handle_upload_prefilter', array($this, 'rename_image') );
  }

  public function main()
  {
    wp_enqueue_script( 'ajax-upload', plugins_url( 'ajax-upload.min.js' , __FILE__), array('jquery'), null );
    wp_localize_script( 'ajax-upload', 'wpu', array(
        'ajaxurl' => admin_url('admin-ajax.php')
      )
    );

  }

  public function process_upload()
  {
    $upload = wp_handle_upload( $_FILES['imageFile'], array( 'test_form' => false ));

    if( $upload ){
      $p = $this->process_image( $upload['file'], $_POST['message'] );
      var_dump($p);
    }
  }

  public function rename_image( $file )
  {
    $info = pathinfo($file['name']);
    $file['name'] = md5($file['name']) . $info['extension'];
    return $file;
  }

  private function process_image( $image, $message )
  {
    $msg = base64_encode( $message );
    $img = imagecreatefromjpeg( $image );
    Morpheus\Data::write($msg, $img);
    imagejpeg( $img, $img );
    return true;
  }

}

new MyClass;

I've omitted the shortcode part, during the debug before I add the filter, all was working fine.

Error log

PHP Fatal error:  Uncaught Error: Call to a member function write() on null in /Users/ziobru/Sites/hereim/wp-content/plugins/im/vendor/pyrou/morpheus/src/Morpheus/Data.php:10\nStack trace:\n#0 /Users/ziobru/Sites/hereim/wp-content/plugins/im/im.php(74): Morpheus\\Data::write('ZmVkZXJpY2Egc2N...', false)\n#1 /Users/ziobru/Sites/hereim/wp-content/plugins/im/im.php(57): MyClass->process_image(NULL, 'fesca...')\n#2 /Users/ziobru/Sites/hereim/wp-includes/class-wp-hook.php(286): MyClass->process_upload('')\n#3 /Users/ziobru/Sites/hereim/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)\n#4 /Users/ziobru/Sites/hereim/wp-includes/plugin.php(465): WP_Hook->do_action(Array)\n#5 /Users/ziobru/Sites/hereim/wp-admin/admin-ajax.php(182): do_action('wp_ajax_nopriv_...')\n#6 {main}\n  thrown in /Users/ziobru/Sites/hereim/wp-content/plugins/im/vendor/pyrou/morpheus/src/Morpheus/Data.php on line 10, referer: http://localhost/hereim/

  • Can you find the actual error message in your server or PHP logs, by enabling WP_DEBUG in wp-config.php? I'd also try tracing out $file and $info from your handler to check what you're getting there in case e.g. 'name' is missing, and / or add some code to handle that case gracefully anyway. – Rup Jan 7 at 13:00
  • I'm using the built-in apache of macOS so I don't have a log to check. On the web I've read that the filter is deprecated from wp 5.1 so maybe this is the problem?I've disabled it and the code started working again ? – sialfa Jan 7 at 13:05
  • I'm not sure it is deprecated? By default the log file is in /var/log/apache2/error_log. – Rup Jan 7 at 13:16
  • I've updated the question with the log – sialfa Jan 7 at 13:35

Your Answer

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

Browse other questions tagged or ask your own question.