When you add an image with "Add media" to the WYSIWYG editor the link around the image automatically gets a rel="attachment wp-att-XX". I would like to remove this with a script - Can anyone help me? :)

  • Why do you want this removed? It's useful information TinyMCE may be using, that has no impact on the frontend..
    – Tom J Nowell
    Mar 1, 2013 at 18:00
  • 1
    Because the site will not validate if it is printed :) the solution posted by Max Yudin is perfect - because it allows TinyMCE to still use the info
    – Cody
    Mar 2, 2013 at 10:01
  • That seems like an awful waste of time and money, I hope you've been charging your client for the faffery around this non-essential change?
    – Tom J Nowell
    Mar 2, 2013 at 14:36

2 Answers 2


You can remove it right before post is printed to the screen by stripping it out from content. But remember it still will meddle in Editor.

function my_remove_rel_attr($content) {
    return preg_replace('/\s+rel="attachment wp-att-[0-9]+"/i', '', $content);
add_filter('the_content', 'my_remove_rel_attr');
  • See birgire's answer for better add_filter().
    – Max Yudin
    Mar 2, 2013 at 10:55
  • you version did the trick :) Could this code be modified to remove the whole link?
    – Cody
    Mar 2, 2013 at 13:18
  • You need different pattern for preg_replace(). But I'm not a guru in regular expressions. Also it's off-topic here.
    – Max Yudin
    Mar 2, 2013 at 16:30
  • Here you are:$pattern = '{(<a\s[^>]+>).?(<img\s[^>]+>).?(</a>)+}Ui'; $output = preg_replace($pattern, '$2', $image_inside_link);
    – Max Yudin
    Mar 2, 2013 at 22:05
  • Take care of attributes, they are in different order sometimes.
    – Max Yudin
    Mar 2, 2013 at 22:06

I guess you mean instead of this html:

<a href="http://example.com/?attachment_id=1375" rel="attachment wp-att-1375"><img src="http://example.com/wp-content/uploads/2012/09/image-300x78.jpg" alt="image.jpg" width="300" height="78" class="alignnone size-medium wp-image-1375" /></a>

you want:

<a href="http://example.com/?attachment_id=1375" ><img src="http://example.com/wp-content/uploads/2012/09/image-300x78.jpg" alt="image.jpg" width="300" height="78" class="alignnone size-medium wp-image-1375" /></a>

in your editor when you press "Insert into post".

Then you can try to use the filter image_send_to_editor and replace the rel-part of the html that is inserted into the editor:

add_filter('image_send_to_editor', 'wpse_88984_remove_rel', 10, 2);
function wpse_88984_remove_rel($html, $id) {
        $html=str_replace('rel="attachment wp-att-'.$id.'"','',$html);

    return $html;   
  • 1
    Yes, you are right. It's better to hook to image_send_to_editor. But I still prefer preg_replace against messing with $id.
    – Max Yudin
    Mar 1, 2013 at 18:17
  • 2
    yes I agree with the preg_replace, maybe he should use the mix of both answers ;-)
    – birgire
    Mar 1, 2013 at 18:31
  • would not do it as the editor might use that information at some point. Max's answer is probably safer Jul 13, 2021 at 15:19
  • yeah, I would most likely leave the attribute alone altogether, if I was looking into this today :-)
    – birgire
    Jul 13, 2021 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.