Excellent approach and thanks!

Only issue I see is that this adds the rel attribute to both image links and attachment links. Wouldn’t it be better if it only applied this attribute to image links?