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
-
1Because 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– CodyMar 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
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.
<?php
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');
-
-
you version did the trick :) Could this code be modified to remove the whole link?– CodyMar 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. Mar 2, 2013 at 16:30 -
Here you are:
$pattern = '{(<a\s[^>]+>).?(<img\s[^>]+>).?(</a>)+}Ui'; $output = preg_replace($pattern, '$2', $image_inside_link);
Mar 2, 2013 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) {
if($id>0)
$html=str_replace('rel="attachment wp-att-'.$id.'"','',$html);
return $html;
}
-
1Yes, you are right. It's better to hook to
image_send_to_editor
. But I still preferpreg_replace
against messing with$id
. Mar 1, 2013 at 18:17 -
2yes I agree with the preg_replace, maybe he should use the mix of both answers ;-)– birgireMar 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 :-)– birgireJul 13, 2021 at 15:53