Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Created June 27, 2022 00:10
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 peterwilsoncc/de643a6f3ae3271cb2ab6c47f6e2fdf4 to your computer and use it in GitHub Desktop.
Save peterwilsoncc/de643a6f3ae3271cb2ab6c47f6e2fdf4 to your computer and use it in GitHub Desktop.
<?php
namespace PWCC\QV_Filter;
/**
* Test if query string contains phrase in deny list.
*
* On a production site this would need to be more complex.
*
* @return bool True: Deny request. False: Allow request
*/
function deny() {
$query_string = $_SERVER['QUERY_STRING'] ?? '';
$deny = false;
$deny_list = [
'pwcc',
];
foreach( $deny_list as $deny_item ) {
if ( false === stripos( $query_string, $deny_item ) ) {
continue;
}
$deny = true;
break;
}
return $deny;
}
/**
* Send headers.
*/
add_filter( 'status_header', function( $status ) {
return 403;
} );
/**
* Prevent query if deny() returns true.
*/
add_filter( 'posts_pre_query', function( $null ) {
if ( deny() ) {
wp_die( 'Please do not do this' );
}
return $null;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment