1

The following applies to all attchment images on my site:

There is a PNG image attachment, its ID is 519271.

wp_get_attachment_image(519271, 'thumbnail', false, array( 'id' => 'esjb-preview-image' )) returns "".

wp_get_attachment_image_src(519271) returns false.

wp_attachment_is_image(519271) returns true

The file is there in the uploads folder. Folder permissions are 755. File permissions are 644.

The attachment is in the wp_posts table and the data seems to be OK.

The file has a proper thumbnail in the media library, but when I click on it and it opens the attachment details (/wp-admin/upload.php?item=519271), there's no image there, just the default empty image icon. The details are correct though, including the actual file URL.

I tried deactivating all plugins (except the Really Simple SSL) to no effect.

I re-generated thumbnails using the Regenerate Thumbnails plugin, but the problem still exists.

The site has been hacked recently (WPCoreSys (Dolly) Hack), after which I deleted all files, changed my host password, then uploaded a clean WP, checked my database for any entries that do not belong there, so I presume WP core files are unchanged. The only folders I took from the hacked install were the uploads folder and my theme folder, but I made sure there are no corrupted files there. Anyway, I deactivated my current theme for a time and nothing changed.

Any ideas on what could have caused the problem?

Edit.

This is my code that can't get data:

static function get_image() {
        if(isset($_GET['id']) ){
            $img_id = (int) $_GET['id'];
            $data = array(
                'image' => wp_get_attachment_image($img_id, 'thumbnail', false, array( 'id' => 'esjb-preview-image' ))
            );
            wp_send_json_success( $data );
        } else {
            wp_send_json_error();
        }
    }

The PHP class itself, as well as AJAX request, they both work just fine.

Edit 2.

I cloned my site to a sub-domain, created an empty database and imported images from the main site. I.e. the files are just the same. On the cloned site, the code works just fine and retrieves image data. So it looks like the issue in my database, but the wp_posts tables in both databases look identical to me, their structure is the same, the indices are the same, and there's nothing strange in the data...

2
  • Thanks for adding that additional info. Makes much more sense now. If the cloned site is working as it should, and there is a chance the database is the issue, it may make more sense to do a fresh install of Wordpress. I did add a suggestion just in case it is the database being corrupted, if you haven't tried it yet.
    – Lisa
    Oct 31, 2022 at 0:06
  • The question is what could have caused it, since the data seem to be OK, and I've done all checks and repairs I could think of.
    – Artem
    Oct 31, 2022 at 2:45

2 Answers 2

2

Not sure how you're using it, but wp_get_attachment_image_src() returns an array. This should work:

<?php
      $attachmentID = 519271;
      $imageSizeName = "thumbnail";
      $img = wp_get_attachment_image_src($attachmentID, $imageSizeName);
?>

<img src="<?php echo $img[0]; ?>" alt="image">

An alternative to try if it isn't working:

<img src="<?php echo wp_get_attachment_url(519271); ?>" alt="image">

This thread discusses your issue further: wp_get_attachment_image_src always returns false


EDIT:

After reading your edit and seeing the site works elsewhere, if it is indeed a corrupted database, try adding this into your wp-config.php file:

define( 'WP_ALLOW_REPAIR', true );

After you have added the line, visit the database repair page: http://your-site.com/wp-admin/maint/repair.php or if you have an SSL certificate, visit https://your-site.com/wp-admin/maint/repair.php.

More information on things that could help found here: https://wpmudev.com/blog/repairing-corrupted-broken-missing-files-databases-wordpress/

8
  • Lisa, thank your for your answer. wp_get_attachment_image_src(519271) returns false, yet the image exists both in my media library and my database. The actual file exists, too.
    – Artem
    Oct 30, 2022 at 7:00
  • @Artem You need to show how you're using the functions as you could be using them incorrectly. Can you please post the code and how you're using it? It would help to see your code and how you're using it instead of showing just what it's returning.
    – Lisa
    Oct 30, 2022 at 9:29
  • 1
    done, see the edits to my question. The code worked just fine before.
    – Artem
    Oct 30, 2022 at 11:01
  • and, well, actually my question is not about wp_get_attachment_image_src(), since I used the function just to test my code, it's about wp_get_attachment_image()
    – Artem
    Oct 30, 2022 at 11:06
  • 1
    Lisa, thank you for your help in resolving this issue. It turned out that meta_id field in the post_meta didn't have the auto_increment attribute. You were indeed right in saying I should have repaired my database, yet somehow, none of the built-in database tools and WP repair plugins and maintenance tools count this case as a database issue. And it technically isn't. I hope that both your answer and my solution will help someone else, too.
    – Artem
    Nov 1, 2022 at 4:49
1

Culprit found!

The wp_postmeta table's meta_id field didn't have an auto_increment attribute.

I have no idea why the malware would remove the attribute.

How I found the solution:

I was checking if wp_attachment_is('image') would return true or false for a particular image. It returned false, while get_post_mime_type() returned "image\png". Strange.

I turned to the functions' source code and found out that wp_attachment_is() uses get_attached_file(), which, in turn, retrieves post meta for the attachment, namely the '_wp_attached_file' meta. I then saw that the images that got 'false' from wp_attachment_is('image') had no such meta.

Then I tried to insert a row into the table via PHPMyAdmin. It failed because of the duplicate meta_id of 0. That indicated that meta_id field had no auto_increment attribute.

The rest is simple. I added the attribute and now everything works as a charm.

So, the answer to my question is:

wp_get_attachment_image() returned empty string and wp_get_attachment_image_src() returned false because, while the attachment post was valid, it had no '_wp_attachment_file' meta, therefore WordPress had no data on the file to return. In turn, no meta was added to the attachment post because the 'meta_id' field had no auto_increment attribute, resulting in duplicate entry error every time WordPress tried to add meta to posts. The problem was probably caused by a recent hack.

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.