WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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? :)

share|improve this question
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 '13 at 18:00
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 '13 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 '13 at 14:36
up vote 1 down vote accepted

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');
share|improve this answer
Thank you very much! – Cody Mar 2 '13 at 10:01
See birgire's answer for better add_filter(). – Max Yudin Mar 2 '13 at 10:55
you version did the trick :) Could this code be modified to remove the whole link? – Cody Mar 2 '13 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 '13 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 '13 at 22:05

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;   
share|improve this answer
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 '13 at 18:17
yes I agree with the preg_replace, maybe he should use the mix of both answers ;-) – birgire Mar 1 '13 at 18:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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