您是否考虑过改用REST API端点?
e、 g.让我们注册我们的端点:
add_action( \'rest_api_init\', function () {
register_rest_route( \'rollor/v1\', \'/count_messages/\', array(
\'methods\' => \'GET\',
\'callback\' => \'count_messages\'
) );
} );
然后实施:
function count_messages($request) {
return $request[\'projectid\'];
}
现在您可以访问
yoursite.com/wp-json/rollor/v1/count_messages?projectid=123
添加验证
我们甚至可以将其扩展为添加内置验证,并将项目ID放在URL中:
register_rest_route( \'rollor/v1\', \'/count_messages/(?P<projectid>\\d+)\', array(
\'methods\' => \'GET\',
\'callback\' => \'count_messages\',
\'args\' => array(
\'projectid\' => function($param,$request,$key) {
return is_numeric($param);
}
)
) );
现在我们可以访问
yoursite.com/wp-json/rollor/v1/count_messages/123
. 它甚至会用通俗易懂的英语告诉我们是否搞错了。
最后:
var myRequest =
$.ajax({
url: \'https://example.com/wp-json/rollor/v1/count_messages/\' + projectid,
});
myRequest.done(function(data){ console.log(data); });