注意:register_rest_route 被错误调用。 REST API 路由必须在 WordPress 主题中的rest_api_init 操作错误上注册

问题描述 投票:0回答:2

我对由我们的网站开发人员开发的旧主题有疑问,该开发人员已无法联系。由于以下错误,我们无法更新到最新版本的 WordPress:

注意:register_rest_route 被错误调用。 REST API 路由必须在rest_api_init 操作上注册

查看我们的主题functions.php文件,我认为这部分导致了问题:

/**
 * Register menu routes for WP API v2.
 *
 * @since  1.2.0
 */
public function __construct() {

    register_rest_route( self::get_plugin_namespace(), '/menus', array(
        array(
            'methods'  => WP_REST_Server::READABLE,
            'callback' => array( $this, 'get_menus' ),
        )
    ) );

    register_rest_route( self::get_plugin_namespace(), '/menus/(?P<id>\d+)', array(
        array(
            'methods'  => WP_REST_Server::READABLE,
            'callback' => array( $this, 'get_menu' ),
            'args'     => array(
                'context' => array(
                    'default' => 'view',
                ),
            ),
        )
    ) );

    register_rest_route( self::get_plugin_namespace(), '/menu-locations', array(
        array(
            'methods'  => WP_REST_Server::READABLE,
            'callback' => array( $this, 'get_menu_locations' ),
        )
    ) );

    register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array(
        array(
            'methods'  => WP_REST_Server::READABLE,
            'callback' => array( $this, 'get_menu_location' ),
        )
    ) );
}

我尝试添加

'permission_callback' => '__return_true',
但这似乎不起作用。

如有任何建议,我们将不胜感激。

wordpress wordpress-rest-api
2个回答
0
投票

好的,我想我已经通过更改代码解决了这个问题,并使用此链接作为指导:

https://github.com/unfulvio/wp-api-menus/blob/master/includes/wp-api-menus-v2.php


0
投票

这是一个通知,而不是一个错误。它不应该阻止您更新 WordPress。

包含您的代码的类应该在

rest_api_init
钩子内实例化,但目前还没有,这就是您收到该通知的原因。

add_action( 'rest_api_init', 'the_function_that_instantiates_your_class' );
© www.soinside.com 2019 - 2024. All rights reserved.