如何在 WP 中调用 rest API 进行自定义路由


how to call rest API in WP for custom routing

我是wordPress的新手,我怀疑如何在WP中调用rest API进行自定义路由,任何人都可以提供示例或给我建议如何做到这一点,任何帮助非常感谢

请尝试 将

自定义路由添加到 WORDPRESS REST API

首先创建一个虚拟链接:

 //Add a Virtual Links
    add_action( 'init', 'wpse9870_init_internal' );
    function wpse9870_init_internal()
    {
      global $wp_rewrite;
        $wp_rewrite->add_external_rule( 'my-api.php', 'index.php?api=1', 'top');
    }
    add_filter( 'query_vars', 'wpse9870_query_vars' );
    function wpse9870_query_vars( $query_vars )
    {
        $query_vars[] = 'getrequest';
        return $query_vars;
    }
    add_action( 'parse_request', 'wpse9870_parse_request' );
    function wpse9870_parse_request( &$wp )
    {
        if ( array_key_exists( 'getrequest', $wp->query_vars ) ) {
            include 'my-api.php';
            exit();
        }
        return;
    }

之后,您可以在 my-api 上编写代码.php对于 restApi,您的请求 url 为:http://siteURL.com/?getrequest