6

UPDATE 2018: The plugin listed below no longer works with WP (as of version 4.5 I believe) but the code listed in the accepted answer DOES still work, so use that instead.

UPDATE: just found this plugin which does what I wanted and more: https://wordpress.org/plugins/b09-link-to-existing-content/faq

I'm pulling my hair out with this, it seems so simple, but there is no easy way in WP (that I can find), and no plugin that will do the following:

In a post, I want to select some text, and then add a link to an existing (full size) image in my media library. I DON'T want to insert an image into my post.

The only way I have found to be able to do this is to find the image, copy the file link, then go to insert/edit url and paste it in. There should be a way to find it the same way you find pages or posts to link to in that same dialog. (OR maybe there is, and I simply can't find it because I am an idiot.)

  • I don't think there's a way to do it without a plugin. I do it the same way you do. Find the image first, copy the image url, and add to the link. – gdaniel May 20 '15 at 18:03
  • Do you know of any plugin that does this? I have scoured the plugin db but haven't found anything that extends that insert/edit dialog (which I guess would be the best way to do it). – Stephen May 20 '15 at 18:09
4

I found a solution based on the code of this page: https://core.trac.wordpress.org/ticket/22180

All attachment files have a post status of 'inherit'. So first you need to add "inherit" as one of the possible post status to search for. You can use the wp_link_query_args filter to do that.

function my_wp_link_query_args( $query ) {

     if (is_admin()){
          $query['post_status'] = array('publish','inherit');
     }

     return $query;

}

add_filter('wp_link_query_args', 'my_wp_link_query_args'); 

By default the url you would get would be the attachment url, and not the file url. So if you need the file url you can use the filter wp_link_query to filter the results.

function my_modify_link_query_results( $results, $query ) {

  foreach ($results as &$result ) {
    if ('Media' === $result['info']) {
      $result['permalink'] = wp_get_attachment_url($result['ID']);
    }
  }

  return $results;

}

add_filter('wp_link_query', 'my_modify_link_query_results', 10, 2);

The foreach loops through all results, find the ones that have a type of Media, and replaces the attachment URL with the file url.

  • 1
    I think this is more slick +1 than my solution (even though they're using same hooks) so I've deleted mine to favor yours ;-) Good findings! – birgire May 20 '15 at 20:11
  • @birgire I started writing my answer, and only saw yours after I published mine. Thanks! – gdaniel May 20 '15 at 20:32
  • btw, I think this is worth turning into a plugin for other people. Let me know if you want to do it and if not, I can create the plugin and upload it to the repo. Thanks again! – Stephen May 20 '15 at 21:00
  • Feel free to do it. – gdaniel May 20 '15 at 21:12
  • FYI, just found this plugin which does what I wanted and more: wordpress.org/plugins/b09-link-to-existing-content/faq – Stephen May 24 '15 at 11:30

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.