I was recently working on an website using an abandoned plugin whose functionality I needed. I noticed however, that all rewritten URLs defined by the plugin return HTTP 200
regardless of if the references resource existed. I tracked the issue down to the code in the parse_request
action handler doing nothing after failing to find the resource. So it needed to trigger/raise the 404 status, and to do so was as simple as setting $query->query_vars['error'] = '404';
. See an example snippet bellow.
add_action( 'parse_request', 'custom_parse_request' ); function custom_parse_request( $query ) { $query_item_exists = false; // implement code to lookup item based on query if ( $query_item_exists ) { // handle changes to query } else { // trigger 404 response $query->query_vars['error'] = '404'; } }
After this change, WordPress correctly returned HTTP 404
when the resource did not exist, and the 404.php
was correctly used.