0

I'm creating a custom plugin. Recently, it's created errors and I don't know the cause.

When the plugin is activated I receive this error in the Site Health Screen:

The REST API call gave the following unexpected result: (404) {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}.

if I navigate to /wp-json I get:

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

When I deactivate the plugin the error is gone from Site Health and I can see content at /wp-json.

I've tried to figure it out and research for hours to no avail. I read about .htaccess problems so I deleted mine and regenerated it. But I know it's related to the plugin somehow.

Any ideas?

2

I found the solution and will post here on the off-chance it helps someone in the future.

The bottom line is there was a typo in a function that responded to POST requests.

I narrowed it down to one file by disabling portions of my plugin in the activation class. By doing this I determined the error only occurred when this file/function was included:

if ( $_SERVER['REQUEST_METHOD'] = 'POST' ) {
    if ( !empty($_POST['update']) ) {
        if ( $_POST['update'] == "clear") {
            $now = new DateTime;
            $now->setTimezone(new DateTimeZone(get_option('timezone_string')));
            update_option('time-tracker-sql-result', array('result'=>'success','updated'=>$now->format('m-d-Y g:i A'),'error'=>'N/A', 'file'=>"", 'function'=>""));
        }
    }
} 

The problem is in my first line, I had = instead of == The corrected code is:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    if ( !empty($_POST['update']) ) {
        if ( $_POST['update'] == "clear") {
            $now = new DateTime;
            $now->setTimezone(new DateTimeZone(get_option('timezone_string')));
            update_option('time-tracker-sql-result', array('result'=>'success','updated'=>$now->format('m-d-Y g:i A'),'error'=>'N/A', 'file'=>"", 'function'=>""));
        }
    }
} 

Once this was fixed, the error disappeared and my /wp-json was visible again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.