我使用WP-REST API插件找到了最简单的解决方案,首先在您的环境中设置:
1)在您的主题中functions.php 注册API端点挂钩:
add_action( \'rest_api_init\', \'register_api_hooks\' );
// API custom endpoints for WP-REST API
function register_api_hooks() {
register_rest_route(
\'custom-plugin\', \'/login/\',
array(
\'methods\' => \'POST\',
\'callback\' => \'login\',
)
);
function login() {
$output = array();
// Your logic goes here.
return $output;
}
2.)默认情况下,如果您启用了相当长的永久链接,WordPress REST API“存在”在/wp json/中。然后可以在以下位置访问API端点:
youdomain.com/wp-json/custom-plugin/login 使用
POST 要求
请注意custom-plugin/login 实际定义为register_rest_route 在PHP函数中register_api_hooks()
对于API键,我使用Wordpress Nonces-pretty straightforward as in my discussion here . 我希望这些答案对所有刚接触Wordpress REST API的全栈开发人员都有用