最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest api - Using Wordpress RESTapi to call a php file instead of post or page

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 3

Make 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;
}
发布评论

评论列表(0)

  1. 暂无评论