最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

404 error - wp-json Returns 404 Upon Plugin Activation

programmeradmin0浏览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?

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?

Share Improve this question asked Nov 4, 2020 at 21:19 AmyAmy 436 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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!

发布评论

评论列表(0)

  1. 暂无评论