admin管理员组文章数量:1315326
I've used WordPress for a few years now and I'm wanting to set up a WordPress instance and use it for API calls. I've never used the RESTapi capabilities of WordPress before. I've done some tutorials and can call pages and post. Instead of calling a page or a post I just want to do a POST request to a file instead that will then run some logic.
My projects goal is going to have videos using iframes running on other peoples webpage and when a user hits play on that video it will do an AJAX call to the API and post some details like the video id, name, website it was watched on ect. I want the logic to then email a customer who filmed that video.
I will use the backend to create a DB of users relevant to the video Ids.
Is this the correct use for the RESTapi and can I call just a file instead of posts and pages or should I be doing this another way? I'm not wanting a front-end for this instance. Just CMS and api calls.
I've used WordPress for a few years now and I'm wanting to set up a WordPress instance and use it for API calls. I've never used the RESTapi capabilities of WordPress before. I've done some tutorials and can call pages and post. Instead of calling a page or a post I just want to do a POST request to a file instead that will then run some logic.
My projects goal is going to have videos using iframes running on other peoples webpage and when a user hits play on that video it will do an AJAX call to the API and post some details like the video id, name, website it was watched on ect. I want the logic to then email a customer who filmed that video.
I will use the backend to create a DB of users relevant to the video Ids.
Is this the correct use for the RESTapi and can I call just a file instead of posts and pages or should I be doing this another way? I'm not wanting a front-end for this instance. Just CMS and api calls.
Share Improve this question asked Nov 20, 2020 at 13:45 monkeyman905monkeyman905 398 bronze badges 1- It makes less sense to think of it as calling/running a file, and more sense to think of it as running code. The code you want to run doesn't have to be in its own file. It sounds like what you're asking is how to create a custom endpoint to execute your own code – Tom J Nowell ♦ Commented Nov 20, 2020 at 14:01
1 Answer
Reset to default 3Make custom API endpoint and move the logic from your PHP file into there instead
For example, the code below will create an endpoint in /wp-json/my/v1/video/<id>
add_action( 'rest_api_init', function() {
register_rest_route( 'my/v1', '/video/(?P<id>\d+)', [
'methods' => 'GET',
'callback' => 'get_video_data',
'permission_callback' => '__return_true',
] );
} );
function get_video_data( $params ) {
$video_id = $params['id'];
// do something like calling the DB where your video data is stored
// you can use custom post type to store the data
return $video_id;
}
本文标签: rest apiUsing Wordpress RESTapi to call a php file instead of post or page
版权声明:本文标题:rest api - Using Wordpress RESTapi to call a php file instead of post or page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741977063a2408186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论