It's probably super easy. I know it can be done when uploading stuffs to WP but I wonder if there is a simple snippet or setting to automatically deactivate link on attachment.

For example :

I do not want : <a href=""><img src="" /></a>but I want <img src="" /> I've veen thinking about a regex to do this but maybe there's a less tricky way to do that.

Hope this is not a duplicate, I've been searching on wpse, found nothing related. Thanks in advance.


With markup embedded into the content there is no less tricky way than regex to do it, save perhaps a markup parser like SimpleHTMLDom

function remove_image_link_102512($content) {
  $pattern = '|<a.*?href="(.*)".*>?(<img.*?/?>)(?:</a>)?|';
  $content = preg_replace($pattern,'$2',$content);
  return $content;

As with all regex + markup solutions, I'd test that thoroughly. Of course, as no saved data is updated it won't catastrophically or permanently break anything if it fails.

| improve this answer | |
  • I see. Thanks a lot. I was thinking about something that works with wp_insert_post_data but this snippet works ! – JMau Jun 10 '13 at 13:20
  • You would need to do about the same with wp_insert_post_data. Altering the content on save, if that is an option, would mean less work for the server with the visitor loads the page. I am sure there are media hooks as well, but I do not know that subsystem especially well. – s_ha_dum Jun 10 '13 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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