Last active
April 8, 2020 08:37
-
-
Save IAmAdamTaylor/81b92080dce7ba95fe2a10f96f69f124 to your computer and use it in GitHub Desktop.
Show WordPress Media Attachments in Insert/Edit Link dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Main plugin file. | |
* | |
* @package Show Media Attachments in Link Dialog | |
* @license GPL2 | |
* Author: Adam Taylor <hello@adamtaylor.dev> | |
*/ | |
/** | |
* Plugin Name: Show Media Attachments in Link Dialog | |
* Description: Adds media attachments with specific extensions to the search window in the Insert/Edit Link dialog. If chosen, a download link to the attachment will be inserted. Filters are available to decide which media attachments are shown. | |
* Plugin URI: https://gist.github.com/IAmAdamTaylor/81b92080dce7ba95fe2a10f96f69f124 | |
* Version: 1.0.0 | |
* Author: Adam Taylor <hello@adamtaylor.dev> | |
* Author URI: https://adamtaylor.dev | |
* License: GPL2 | |
*/ | |
/* Copyright 2020 Adam Taylor (email : hello@adamtaylor.dev) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
add_filter('wp_link_query_args', 'smalg_modify_link_query_args'); | |
function smalg_modify_link_query_args( $query ) { | |
// include 'inherit' post statuses so attachments are returned | |
$query['post_status'] = (array) $query['post_status']; | |
$query['post_status'][] = 'inherit'; | |
// use post query filters | |
$query['suppress_filters'] = false; | |
// use custom query arg detectable in other filters | |
$query['smalg__media_last'] = apply_filters('smalg_show_media_last', true); | |
$allowed_exts = apply_filters('smalg_allowed_extensions', array('pdf')); | |
$meta_query = array( | |
'relation' => 'OR', | |
'meta_not_exists' => array( | |
'key' => '_wp_attached_file', | |
'compare' => 'NOT EXISTS', | |
), | |
'ext_like' => array(), | |
); | |
foreach ($allowed_exts as $key => $ext) { | |
$meta_query['ext_like'][] = array( | |
'key' => '_wp_attached_file', | |
'value' => '.'.$ext, | |
'compare' => 'LIKE', | |
); | |
} | |
if ( isset($query['meta_query']) && $query->meta_query ) { | |
// merge meta queries | |
$query['meta_query'] = array( | |
'relation' => 'AND', | |
$query['meta_query'], | |
$meta_query | |
); | |
} else { | |
$query['meta_query'] = $meta_query; | |
} | |
return $query; | |
} | |
// link to media file's permalink instead of attachment page | |
add_filter( 'wp_link_query', 'smalg_modify_link_query_results', 10, 2 ); | |
function smalg_modify_link_query_results( $results, $query ) { | |
return array_map(function($r) { | |
if ( 'Media' === $r['info'] ) { | |
$permalink = wp_get_attachment_url( $r['ID'] ); | |
$path = parse_url( $permalink, PHP_URL_PATH ); | |
$ext = pathinfo( $path, PATHINFO_EXTENSION ); | |
$r['title'] = $r['title'].'.'.$ext; | |
$r['permalink'] = wp_get_attachment_url($r['ID']); | |
} | |
return $r; | |
}, $results); | |
} | |
// high priority to ensure it runs after some plugins like Post Types Order | |
add_filter('posts_orderby', 'smalg_order_search_by_posttype', 900, 2); | |
function smalg_order_search_by_posttype( $orderby, $wp_query ){ | |
if( | |
$wp_query->is_admin && | |
isset($wp_query->query['smalg__media_last']) && | |
$wp_query->query['smalg__media_last'] | |
) { | |
global $wpdb; | |
// order as: not attachments by post date DESC -> attachments | |
$orderby = " | |
CASE WHEN {$wpdb->prefix}posts.post_type = 'attachment' THEN '__' | |
ELSE {$wpdb->prefix}posts.post_date END DESC"; | |
} | |
return $orderby; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the things that's always annoyed me about WordPress's Insert Link dialog window is that you can't search for PDFs and other attachments in your media library. The workaround to upload the file to the Media Library, open it and copy-paste the URL has always felt clunky to me.
This plugin makes that issue go away by adding media attachments with specific extensions into that search window! It also works with ACF Link fields as they use the same Insert Link dialog.
By default only PDFs are shown, however you can filter this to include others such as
.docx
if needed.To install
/wp-content/mu-plugins/
and add the code from the Gist.mu-plugins
WordPress will start using it straight away, no need to activate.Filters
There are 2 filters available to use in this plugin:
smalg_allowed_extensions
Filter the list of file extensions that decide which media attachments are shown in the search dialog.
Defaults to:
array('pdf')
When filtering, make sure to return an array and do not include the period before the extension, i.e. 'docx' instead of '.docx'.
smalg_show_media_last
To keep the search dialog window clean, media attachments are shown last by default. You can use this filter to turn off that behaviour and show the search dialog list in newest to oldest posts order (the WordPress default).
Defaults to:
true