Arthur

Pemberton

Full-stack web applications developer


Welcome to my blog

As I discover new things in my field and solve troublesome problems, I will attempt to document my finds here.

Trigger WordPress 404 when using parse_request

On September 13, 2022, by Arthur Pemberton, 0 Comments

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.

Changing the “Archive Title” used by WordPress

On July 21, 2015, by Arthur Pemberton, 0 Comments

If you are using an SEO plug-in with WordPress, you may have changed the <title> of one or more of your archives pages. However, the theme you are using is probably making use of the get_the_archive_title() WordPress function to get the title that is used in, for example, the <h1>.
Read More