Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.