Hitting WordPress Attachment Handling
In debugging some stuff here on WordPress.com, I dived (once more) into the image handling guts of WordPress.
A while ago, I revamped WordPress’ inline uploading functionality to be more pluggable, cacheable, maintainable and (hopefully) intuitive. A hundred bugs later, it seems to be working pretty well.
This time around, the issue was not how WP displayed the data, but how it stores it.
In trying to make things leaner and meaner on WordPress.com, we reworked some of the behind-the-scenes directory structures on our servers. This should have been totally transparent to the user, but, of course, it wasn’t. It broke our image handling.
Each file uploaded to WordPress is stored as a special type of post called an “attachment”. In the post and postmeta tables, WordPress stores things like file URL, file path on the server, image dimensions, image thumbnail info, etc. So to figure out anything we want about an uploaded file, all we have to do is essentially get_post() and a few get_post_meta()s. Sounds great.
It’s not.
Two reasons:
- To answer a question like “Where is this file located on the server”, we have to
get_post_meta()every time for the answer. With such a generic function, it’s impossible for plugins to filter that data on the fly even for this really simple question. - WordPress stores the data in a really inconvenient way; everything is absolute: paths, urls, you name it.
Because of the absolute paths, lack of API, and the aforementioned directory restructuring, we had hardcoded into our DB tens of thousands (a blind guess) of incorrect file locations and no way to filter them. Doing DB updates across hundreds of thousands of tables (not a blind guess) replicated on dozens of servers was not an option.
Partial solution (now implemented in WordPress core): Write a basic API for getting and putting the data. wp_get_attachment_metadata(), wp_update_attachment_metadata(), get_attached_file(), update_attached_file() (the lack of parallelism between function names, and the ambiguity in those names is an historical artifact) are all nicely filterable.
Improvements that still could be made: Don’t store absolute data. This is better for portability too. (Exceptition: guid – but don’t use it for the URL).
Related improvements that could be made:
wp_get_attachment_url()wp_get_thumbnail_file()wp_get_thumbnail_url()wp_attachment_is_image()wp_mime_type_icon()- Rework
get_attachment_icon()given the above - Kill reliance on or rework
get_attachment_innerHTML()(see above)
Vaguely related improvements that could be made:
wp_handle_upload()is a little awkward (but nice and robust!). Maybe wrap it? That might be silly.wp_generate_thumbnail_filename()- Make thumbnail creation hookable and provide above convenience function
Update: All of the “related improvements” noted above have been made in the recently released WordPress 2.1. Of the “vaguely related improvements” suggested, 2.1 offers hookable thumbnail creation.
December 8, 2006 at 1:56 am
I’ve got a small beef with the way files are uploaded- once you put in a title and a description – then you can’t ever get back to edit them- bad UI. Should be in manage/attachments area- just like pages, posts, comments etc.
I’ve also had problems lately with pdf’s not building correct links- I end up with the upload going to the right place/uploads/year/month- but the link to the PDF is just to WP-content or some such-
The link to file, link to page, link to image interface is also a little convoluted- seems that we should be able to tag pictures- and “Caption” them- instead of having to put them on a separate page-
if there was a one size all file upload solution- I”m sure someone would have figured it out- but, I know that you guys are really smart and knew all this already.
December 8, 2006 at 6:22 pm
David,
We’v fixed several of those problems in the upcoming WordPress 2.1 (which is not yet in beta). So you have something to look forward to in the next couple months!
January 29, 2007 at 9:57 am
Hi mdawaffe,
That was a nice job you did. I like this API. I have wrote attachment management helper plugin for WordPress 2.0 and now am rewriting it for new 2.1 version. I found the API so helpful.
By the way, it seems that wp_get_attachment_url() still refers guid for the URL. Doesn’t this harm portability? Are there existing patches on Trac for this?
January 29, 2007 at 10:14 am
miyoshi,
I’m glad you found the API useful.
The old use of the guid as the URL was one of the main reasons this API was written. In a future release, it will be very easy to rewrite the insides of wp_get_attachment_url() to grab the URL from somewhere else. Since only the insides of the function will change, plugins that use the API will not break.
October 13, 2007 at 3:28 pm
[...] not, however, Michael Adams, in his blog listed the available API functions for attachments last [...]
November 10, 2008 at 7:14 pm
i love your blog.Little article but these are very nice
December 16, 2008 at 5:02 am
deutsch online based kasino…
unevaluated humble,starfish …
January 26, 2009 at 12:59 am
downtown pokar…
stipulations reminded?lavender …
March 9, 2009 at 1:47 pm
徵信社的服務項目,如婚前徵信、商業徵信、財產徵信等等
March 9, 2009 at 1:49 pm
採購OA辦公家具或要重新室內裝潢設計,可以找信譽優良且值得信賴的三多麗室內設計公司,
March 9, 2009 at 1:49 pm
提升托福或多益考試成績的最佳選擇—哈佛英文補習班,透過它專業的訓練,讓您輕鬆達成目標。
March 9, 2009 at 1:51 pm
在坐月子期期,若要依照坐月子餐食譜準備月子餐,不但費時且辛苦,喜樂月子餐外送服務,可以代勞。
March 9, 2009 at 1:59 pm
托福考試為全球施行最廣的英語能力檢定toefl測驗
March 9, 2009 at 2:01 pm
提供花蓮民宿旅遊、台東民宿、花東旅遊、景點活動與交通租車等的資訊。
March 9, 2009 at 2:02 pm
提供辦公文具禮品、電子製品、家用品、生活雜貨及玩具等贈品批發製造
March 9, 2009 at 2:08 pm
團體制服能讓人第一眼即留下深刻的印象,所以選製團體服是不能隨便,好的制服會讓人在平時也會想要穿上,優可制服公司就是提供這樣高質感的服務。
March 9, 2009 at 2:08 pm
菁英IELTS補習班擁有豐富的教學經驗,教授雅思的考試技巧,提供完整資料讓應試者能快速提高雅思成績。
March 9, 2009 at 2:10 pm
菁英留學代辦中心,擁有專業的留學顧問群,凡有出國留學的任何問題,都歡迎電洽或親洽,尤其美國留學及英國留學。
March 9, 2009 at 2:11 pm
不論您要英國遊學、紐西蘭遊學、澳洲遊學、加拿大遊學或美國遊學,菁英遊學代辦中心都能提供最專業的資訊及服務。
March 9, 2009 at 2:12 pm
純白室內設計公司提供室內設計、商業空間設計、室內裝潢等的規劃、施工服務
March 9, 2009 at 2:12 pm
專業的會計師事務所,讓會計師成為您事業的得力助手。
March 9, 2009 at 2:14 pm
oa辦公設備如影印機、印表機及傳真機,現在都成為一台多功能的事務機就可以具備以上功能。
March 9, 2009 at 2:15 pm
專業的toeic補習班讓您擊破新式多益考試。
March 9, 2009 at 2:17 pm
寶威提供室內裝修需要的高品質馬賽克、磁磚 (瓷磚)及拋光磚等
March 9, 2009 at 2:18 pm
提供多益(TOEIC)、全民英檢、IELTS、英會、線上測驗學習及企業訓練課程等
March 9, 2009 at 2:19 pm
Ladybuglight提供由日本專業設計師為新人量身打造的獨特喜帖,另有馬克杯設計。
March 9, 2009 at 2:20 pm
提供有關室內設計、住宅別墅設計、商業空間設計、室內裝潢裝修工程等服務。