Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding the attachment_id even after site migration breaks the link between url and guid #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TNG02
Copy link

@TNG02 TNG02 commented Dec 8, 2014

Alteration to find the attachment/post ID from the postmeta table rather than the guid held in posts table.

This (hopefully!) fixes the problem where the url differs from the guid because of site migrations i.e. where images are loaded onto one site (at which point the guid and the url are the same) but then the site is migrated and the url changes but the guid doesn't.

The problem I encountered was that picturefill-wp didn't seem to work at all when I installed it on a site that I had migrated.

Typically we will develop sites on local machines (but not using localhost as the url) and migrate them to testing and development servers where the domain name, obviously, changes.

When media is added it receives a value in the guid fields of the WordPress <wp_>Posts table. This guid is set to the url for the image. When the site is migrated, the guid stays the same e.g. http://my_local_machine/uploads/my_img.jpg but the url changes e.g. http://my_testing_server_domain/uploads/my_img.jpg.

The plugin was set to find the post (attachment) ID for the image using the guid, but because this is no longer the same as the url, it couldn't find the details and set the value of the image_attributes['attachment_id'] property of the Model_Image_Picturefill_WP class to false which meant that the standard (non-picturefill) img markup was used because the srcset array wasn't being populated by the set_serset_array() method.

Instead of using the guid in <wp_posts> to find the post/attachment ID, I used the <wp_>postmeta table finding the attachment path and file name where the meta_key field is equal to '_wp_attached_file'. So for a url 'http://my_domain/uploads/2014/12/my_image.jpg' it extracts '2014/12/my_image.jpg' finds the row for this meta_value and meta_key combination and reads the post_id column.

Disclaimers:

I've added two static 'helper' functions to perform the logic, for ease, I have just made them static functions but wouldn't be particularly happy leaving them here if it was my code.

I can't say I've tested the strip_baseurl function particularly thoroughly

I won't claim to be an expert at WordPress so can't be sure that the link from <wp_>postmeta to <wp_>posts is 100% safe but I haven't been able to break it yet.

…her than the guid held in posts table.

This (hopefully!) fixes the problem where the url differs from the guid because of site migrations i.e. where images are loaded onto one site (at which point the guid and the url are the same) but then the site is migrated and the url changes but the guid doesn't.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant