Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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;
}
add_filter('the_content','remove_image_link_102512');

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.

share|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
    
Yeah I 'll look for it thanks for the pattern ^^ –  JMau Jun 10 '13 at 13:27
add comment

Your Answer

 
discard

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.