1

I'm making a custom wp plugin for image optimization. I'm at the point that you choose the image, the script starts to optimize and put the new image in the upload folder. The problem is that i can't register the new image correctly in the WP Media, i've already seen a lot of posts about that but still i didnt solved.

The main problem is that i can see the file in my wp media, but without thumbnail or other informations like filesize, image width and height.

At the first time my script was uploading his files in /wp-content/uploads/plugin-folder/ , but then i noticed that the problem was that wp_generate_attachment_metadata is making an empty array. So i moved every new image in the correct uploads folder like /wp-content/uploads/2019/09/etc.

The problem was not solved and the function wp_generate_attachment_metadata is still making and empty array.

I noticed that if i create a custom array for the attach data, with only width and height, wordpress generate the media thumbnail, but i still don't get the filesize that is the most important thing for a script that optimize images.

Here is the code:

        require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );
    $filename = $new_file_path;
    $filetype = wp_check_filetype( basename( $filename ), null );
    // Prepare an array of post data for the attachment.
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $filename, 0 );
    echo '<b>' . $attach_id . '</b>' . '<br>';
    // Generate the metadata for the attachment, and update the database record.
    $new_image_info = getimagesize($new_file_url);
    // $uploaded_file_info = get_attached_file($attach_id);
    $custom_attach_data = array(
        'width' => $new_image_info[0],
        'height' => $new_image_info[1],
    );
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
    wp_update_attachment_metadata( $attach_id, $attach_data );
    wp_update_attachment_metadata( $attach_id, $custom_attach_data );
    // echo '<textarea>';
    //  var_dump($attach_data);
    // echo '</textarea>';
    set_post_thumbnail( $attach_id, $attach_id );

As you can see i use 2 times the function wp_update_attachment_metadata, and the second time is my custom array. With this i get the image thumbnail but with still some data missing. I'm sure im missing something.

Thank you in advance.

Update: My image path are now something like:

/var/www/html/demositeurl.com/wp-content/uploads/2019/09/20170610_205517_web_optimized.png

I also noticed that the filesize() function is not working, i get the error:

[24-Sep-2019 14:25:48 UTC] PHP Warning:  filesize(): stat failed for /var/www/html/demosite.com/wp-content/uploads/2019/09/20170610_205517_web_optimized.png in /var/www/html/demosite.com/wp-content/plugins/demo-plugin/demo-main-file.php on line 91

At first i thinked it was a permission problem, but even with everything assignet to www-data:www-data and 775, is still not working. Maybe this can be the real issue?

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.