Created
February 13, 2018 17:34
-
-
Save markjaquith/2c8c29a0ccd2916520e5b71fcec626ca to your computer and use it in GitHub Desktop.
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 | |
/* | |
Plugin Name: Block WordPress attachment queries | |
Description: Blocks MySQL queries for WordPress attachments that are on domains we know will never resolve to a local WordPress attachment ID. | |
Author: Mark Jaquith | |
*/ | |
add_filter( 'query', function( $query ) { | |
global $wpdb; | |
// List the domains that are external and will never resolve to local WordPress attachment IDs. | |
$domains = [ | |
'cdn.example.com', | |
]; | |
// Quote each domain for use in a regular expression. | |
$domains = array_map( function( $domain ) { | |
return preg_quote( $domain, '#' ); | |
}, $domains ); | |
// Break the domains out. | |
$domains = implode( '|', $domains ); | |
// Filter the query. | |
return preg_replace( "#SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = 'https?://($domains)[^']+'#", 'SELECT 0;', $query ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment