Make WordPress Core

Opened 12 years ago

Closed 9 years ago

Last modified 5 years ago

#22180 closed feature request (worksforme)

Add attachments to "existing content" area in link dialog

Reported by: cgrymala's profile cgrymala Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Editor Keywords:
Focuses: Cc:


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:

  1. Upload the file through the media uploader
  2. Press the File URL button in the media modal
  3. Copy the URL from the "Link URL" box
  4. Close the media modal
  5. Highlight the text they want to link
  6. Press the "Insert/edit link" button in the editor toolbar
  7. 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:

  1. Add 'attachment' to the $pts array at the beginning of the _WP_Editors::wp_link_query() method.
  2. 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)

#1 @SergeyBiryukov
12 years ago

  • Version changed from trunk to 3.4

#2 follow-up: @dbarlett
11 years ago

As of 3.7.1 (possibly earlier), attachments are included in $pts, but the query only returns objects with post_status == publish (attachments have post_status == inherit). 3.7.0 added the wp_link_query_args filter, so this works:

add_filter( 'wp_link_query_args', 'my_modify_link_query_args' );

// Query for all post statuses so attachments are returned
function my_modify_link_query_args( $query ) {
	$query['post_status'] = 'any';
	return $query;

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:

add_filter( 'wp_link_query', 'my_modify_link_query_results' );

// Link to media file URL instead of attachment page
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;

#3 in reply to: ↑ 2 @helen
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 @iseulde
9 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 @bjornenblog
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 @pomegranate
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;

#7 @wpfangirl
5 years ago

When I test this, it works in the Classic Editor but not in the block editor. Is there a different hook to get it to work in the block editor?

Note: See TracTickets for help on using tickets.