我真的无法重现错误。您确定脚本已正确加载吗?
function get_post_details ($params)
{
return rest_ensure_response( \'Hello World, this is the WordPress REST API\' );
}
// Register the rest route here.
add_action( \'rest_api_init\', function () {
register_rest_route( \'post_info\', \'post\',array(
\'methods\' => \'GET\',
\'callback\' => \'get_post_details\'
));
});
这给了我预期的响应-因此路线正确注册在
http://www.example.com/wp-json/post_info/post
至于参数
suggested in the REST API docs, 您应该使用
$params
而不是
$_GET
. 更改此项并添加
$post = array();
(因此正确声明了变量),使您的代码在我的本地计算机上工作。您可能应该首先检查是否设置了这些值
function get_post_details ($params)
{
$id = $params[\'id\'];
$meta_value = $params[\'meta_value\'];
$post = array();
if(update_post_meta( $id, \'_yoast_wpseo_redirect\', $meta_value ))
{
$post[\'status\'] = \'Successfully Updated\';
$post[\'flag\'] = \'1\';
}
else
{
$post[\'status\'] = \'Error\';
$post[\'flag\'] = \'0\';
}
if( empty( $post ) ){
return null;
}
return $post;
}
// Register the rest route here.
add_action( \'rest_api_init\', function () {
register_rest_route( \'post_info\', \'post\',array(
\'methods\' => \'GET\',
\'callback\' => \'get_post_details\'
));
});
以及路线
http://www.example.com/wp-json/post_info/post?id=21&meta_value=https://www.google.com
If you don\'t have pretty permalinks enabled, the route may look different!