Maybe do something with the “request” filter? Your callback is passed all the query vars established by the request. Check if “name” or “pagename” vars are set. If so apply your preg_match() test to determine if extended chars in the slug are requested or not. If so, you can unset/set the vars needed to return a 404 error instead of fulfilling the request.
This works! Thank you.
function filter_qvar_value($value) {
if (preg_match("/^[A-Za-z0-9-]+$/", $value) || $value == '') {
return $value;
} else {
return "404";
}
}
function remove_special_characters( $qvars ) {
return array_map("filter_qvar_value", $qvars);
}
add_filter( 'request', 'remove_special_characters' );