#22180 closed feature request (worksforme)
Add attachments to "existing content" area in link dialog
Reported by: | cgrymala | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.4 |
Component: | Editor | Keywords: | |
Focuses: | Cc: |
Description
At the moment, the process for linking to attachments within the editor is rather cumbersome. Each time someone wants to link to a PDF document or other item they've uploaded through the media interface, they have to take the following steps:
- Upload the file through the media uploader
- Press the File URL button in the media modal
- Copy the URL from the "Link URL" box
- Close the media modal
- Highlight the text they want to link
- Press the "Insert/edit link" button in the editor toolbar
- Paste the URL from their clipboard into the URL input
It would be nice if attachments could be included in the "Link to existing content" area within the link dialog.
I can see two possible ways of implementing this. Either simply include attachments in line with the pages, posts and custom post types that already display in that area; or, use a tabbed interface to switch back and forth between the normal "existing content" list and a list of existing attachments.
When I discussed this with Nacin and a few others at WordCamp Baltimore, we proposed adding a preference (possibly based on the information that's stored when the user uploads the media) determining whether the permalink leads to the attachment page or the actual file.
Based on my preliminary research, it looks like implementing this will essentially take two steps:
- Add 'attachment' to the
$pts
array at the beginning of the_WP_Editors::wp_link_query()
method. - If the preference has been set to link to the actual file, the code will need to adjust the
permalink
value in the$results
array for anything that comes up as an attachment.
I'll look at starting a patch for this over the next few days, unless someone else gets to it first. Thanks.
Change History (7)
#3
in reply to:
↑ 2
@
11 years ago
Replying to dbarlett:
The post_status
arg can take an array, so you can explicitly include 'inherit' rather than just jumping all the way into 'any'.
#4
@
10 years ago
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from new to closed
This is much better since WP 3.5. Inserting PDFs, TXT files etc. from the media modal will give you a link.
#5
@
7 years ago
It does not work. Any of them.
What I'm looking for is the function of searching the media library with "add link" (CTRL + K) feature. I want to be able to link to the PDF file.
#6
@
5 years ago
For anyone else trying to accomplish this, there was a small error in one of the filters above (missing the argument count = 2 in wp_link_query
). This worked for me:
// include 'inherit' post statuses so attachments are returned
add_filter( 'wp_link_query_args', 'my_modify_link_query_args' );
function my_modify_link_query_args( $query ) {
$query['post_status'] = (array) $query['post_status'];
$query['post_status'][] = 'inherit';
return $query;
}
// Link to media file URL instead of attachment page
add_filter( 'wp_link_query', 'my_modify_link_query_results', 10, 2 );
function my_modify_link_query_results( $results, $query ) {
foreach ( $results as $key => $result ) {
if ( 'Media' === $result['info'] ) {
$results[$key]['permalink'] = wp_get_attachment_url( $result['ID'] );
}
}
return $results;
}
As of 3.7.1 (possibly earlier), attachments are included in
$pts
, but the query only returns objects withpost_status == publish
(attachments havepost_status == inherit
). 3.7.0 added thewp_link_query_args
filter, so this works:It has a side effect of showing future, pending, and draft posts. Some users consider that a feature, since it lets them build a series of cross-linked posts to publish simultaneously. If you want to use direct attachment URLs instead of attachment pages, add: