Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Created February 13, 2018 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjaquith/2c8c29a0ccd2916520e5b71fcec626ca to your computer and use it in GitHub Desktop.
Save markjaquith/2c8c29a0ccd2916520e5b71fcec626ca to your computer and use it in GitHub Desktop.
<?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