Remove HTML and CSS on 404 API Route
-
Hello, On my WordPress site, when a anyone tries to call a nonexisting API route, it gives a 404 with all the HTML and CSS used on the page. Can you help with a custom function that does not show all the HTML and CSS used on the page, maybe something that says error instead of the codes displayed.
function custom_handle_api_404() { if (is_admin() || !defined('REST_REQUEST') || !REST_REQUEST) { return; } if (is_404()) { // Check if the request is for an API route $current_uri = $_SERVER['REQUEST_URI']; $api_base = rest_get_url_prefix(); if (strpos($current_uri, $api_base) !== false) { // If the request is for the API route, output a custom error message header("HTTP/1.1 404 Not Found"); header('Content-Type: application/json'); echo json_encode(array('error' => 'API route not found')); exit; } } } add_action('template_redirect', 'custom_handle_api_404');
I have tried using the above, but it doesn’t work
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Remove HTML and CSS on 404 API Route’ is closed to new replies.